• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

need help withscrollingList that highlights words in a list

Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic