This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need help withscrollingList that highlights words in a list  RSS feed

 
vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am trying to learn java on my own and I have a problem with this program.
The program will display a frame containing an empty text field and a list of words in sorted order. The user can enter a word (or a portion of a word) in the text field. Each time the user changes the text field, the program will highlight the first word in the list that begins with the letters in the text field. If the user enters letters tha don't match any word in the list, the highlight should remain where it was previously.
Hint: move the hightlight to that word by calling the select method
wordList.select(i)//wordList is a List object
I don't know what statement I should use for selecting the word in the scrolling list
class ListFrame extends JFrame implements ActionListener{
private JTextField topDisplayField = new JTextField("", 30);
private JList wordList;
private String[] words;
public ListFrame() {
setTitle("ScrollingList");
setSize(400, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JPanel textPanel = new JPanel();
textPanel.add(topDisplayField);
Container contentPane = getContentPane();
topDisplayField.addActionListener(this);
contentPane.add(textPanel, "North");
String[] words = {
"a", "aback", "abandon", "abate", "abbey", "abbot",
"abbreviate", "abdicate", "abdomen", "abdominal",
"abduct", "abduction", "abet", "abreast", "abs"};
JList wordList = new JList(words);
JScrollPane scrollPane = new JScrollPane(wordList);

JPanel p = new JPanel();
p.add(scrollPane);
contentPane.add(p, "Center");
}

public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
String group = "";
int i;
String wordKey = topDisplayField.getText();
for (i = words.length; i > 0; i--)
group = words[i].toLowerCase();
if (group.startsWith(wordKey))
wordList.setSelectedIndex(i);
}
}

public class ScrollingList{
public static void main(String[] args){
JFrame frame = new ListFrame();
frame.show();
}
}
P.S: I have also posted this question in java general(new to java beginner)
Thanks in advance
Vidu
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!