Solved

JList set selected + java

Posted on 2004-03-23
7
401 Views
Last Modified: 2010-04-17
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.

Thanx.


  //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));
          //list.setSelectedIndex(i);
          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));
            list.setSelectedIndex(i);
            list.updateUI();
            return;
          }
        }
      }
    }
0
Comment
Question by:raaz007
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10665714
Is it entering the selectNearest () method? Can we have the code for the listener too?
0
 

Author Comment

by:raaz007
ID: 10668829
Yes, its entering the selectNearest() method. I have said earlier that its not getting into the 2nd if condition. Hope u understood now my problem clearly.

Following is the code where i am calling this method.

 //Testing DocumentListener for JTextField.
    public void removeUpdate(DocumentEvent e) {
    }

    public void insertUpdate(DocumentEvent e) {
      System.out.println("Value typed is ********** " +txt_WorkerName.getText());
      selectNearest(txt_WorkerName.getText(), list_WorkerNames);
    }

    public void changedUpdate(DocumentEvent e) {
    }
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10669403
Is it supposed to be case-sensitive or insensitive? Which part of the selectNearest () method is it going into?
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:raaz007
ID: 10669949
its not at all case sensisitive.......

Its going with inside the 1st IF condition but not going inside the 2nd IF condition of searchNearest() method.

Hope you understood now.....!!!
I feel you're not at all getting the problem clearly. Hope this time you got it.....!!!!  Its failing within the 2nd IF i.e., in the following code if you can see 2nd IF....there its failing......!!!!!!!




      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)) {
                      System.out.println("Coming inside 2nd if ******");
            list.setSelectedIndex(i);
        }
0
 

Author Comment

by:raaz007
ID: 10671163
Did you get the problem now....or not now also..................????
0
 

Author Comment

by:raaz007
ID: 10672205
Please let me know if you're in a position to answer my question..!!!
I am waiting for it.....!!!!!
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 20 total points
ID: 10674316
>> Did you get the problem now....or not now also..................????
>> Please let me know if you're in a position to answer my question..!!!

There is a time-zone difference between us. I saw your comments only now.

>> Its failing within the 2nd IF

Perhaps you can try equalsIgnoreCase () in that case.

>> if (item.substring(text.length()).equals(text))

if ( item.substring ( text.length () ).equalsIgnoreCase ( text ) )

Ok, I found a big mistake. If you pass one argument to the the substring () method, then it will get you a substring from that position to the end of the string, not from the start of the string to that position. So, you should use:

if ( item.substring ( 0, text.length () ).equalsIgnoreCase ( text ) )

- you can check what values the substring () method prints by displaying it on the console, and you can verify whether it matches the 'text'.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question