This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with scrolling list 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();
}
}
thanks in advance
vidu
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got 4 things wrong with this program. (That I can see anyway) Here is the source that will work. See if you can find the problems. If you have questions about why yours was wrong, then repost.

Barry
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum...
 
vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry:
Thank you so much. It is working now. I found all the 4 problems which you mentioned. I cannot beleive that I did not add braces after the for loop
It didn't matter if I put the actionPerformed method in the same class or different class
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!