• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help withscrollingList that highlights words in a list

vidu mayur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() {
setSize(400, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
JPanel textPanel = new JPanel();
Container contentPane = getContentPane();
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();
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))

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