troubleshooting Question

JList set selected + java

Avatar of raaz007
raaz007 asked on
7 Comments1 Solution505 ViewsLast Modified:
Hi all,

I am using the following method in order to select an item in a JList depending upon the text typed in the JTextField. I am using the DocumentListener and adding the listener to the JTextField. Everything works fine but item in JList is not getting highlighted upon the text typed in TextField. Can anyone let me know whats wrong in the code.

In the following code, 2nd if condition is not satisfying.....Any suggestions are welcome on this. Pls treat it as urgent.


  //This method is used to set the username selected in the JList based on the text typed in the textfield.
    public void selectNearest(String text, JList list) {
      String item = null;
      for (int i = 0; i < list.getModel().getSize(); i++) {
        item = (String)list.getModel().getElementAt(i);
        if (text.length() <= item.length()) {
          System.out.println("Value in text is ******* "+text.charAt(0));
          System.out.println("Value in item is ******* "+item.charAt(0));
          if (item.substring(text.length()).equals(text)) {
          //if (text.equals(item.substring(text.length()))) {
            System.out.println("Coming inside 2nd if **********************");
            //System.out.println("Value in item in if 2 is ******* "+text.charAt(0));
            //System.out.println("Value in text in if 2 is ******* "+item.charAt(0));
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros