Avatar of rajah_mohammed
rajah_mohammed

asked on 

Search in JList

Hi;

I have a code below that should search a string and if the string is found should highlight and select the string on the JList:

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.text.Position;

public class ListSearch extends JFrame {

      private JList list;
      private JTextField field;
      private DefaultListModel listModel;
      private StringTokenizer token;
      private BufferedReader br;
      private StringBuffer allText;
      
      public ListSearch() {
            super("ListSeach");
            
            try {
                  
                  br = new BufferedReader(new FileReader(new File("com/employee.txt")));
                  String text = null;
                  allText = new StringBuffer();
                  
                  while((text = br.readLine()) != null) {
                        allText.append(text + "\r\n");
                  }
                  
                  token = new StringTokenizer(allText.toString());
                  
                  listModel = new DefaultListModel();
                  while(token.hasMoreTokens()) {
                        listModel.addElement(token.nextToken());
                  }
            } catch(Exception e) {
                  e.printStackTrace();
            }
            
            list = new JList(listModel);
            field = new JTextField();
            
            getContentPane().add(field, BorderLayout.NORTH);
            getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
            
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400, 300);
            setLocationRelativeTo(null);
            setVisible(true);
            
            field.addKeyListener(new KeyAdapter() {
                  public void keyReleased(KeyEvent e) {
            
                        char text = "" + e.getKeyChar();
                        StringBuffer buffer = new StringBuffer();
                        buffer.append(text);

                        int index = list.getNextMatch(buffer, 0, Position.Bias.Forward);
                        System.out.println(index);
                        list.setSelectedIndex(index);
                  }
            });
      }
      
      public static void main(String[] args) {
            ListSearch h = new ListSearch();
      }
}

My sample text is:

Employee.Arnold.James
Employee.Arnold.Warlburg
Employee.Arnold.Seen

The problem is when the user tries to type any on the text it only selects the 1st one? Can anyone help
Java

Avatar of undefined
Last Comment
Javatm
ASKER CERTIFIED SOLUTION
Avatar of chakra-march-04
chakra-march-04

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Javatm
Javatm
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo