Link to home
Start Free TrialLog in
Avatar of raaz007
raaz007

asked on

JList set selected + java

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;
          }
        }
      }
    }
Avatar of Mayank S
Mayank S
Flag of India image

Is it entering the selectNearest () method? Can we have the code for the listener too?
Avatar of raaz007
raaz007

ASKER

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) {
    }
Is it supposed to be case-sensitive or insensitive? Which part of the selectNearest () method is it going into?
Avatar of raaz007

ASKER

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);
        }
Avatar of raaz007

ASKER

Did you get the problem now....or not now also..................????
Avatar of raaz007

ASKER

Please let me know if you're in a position to answer my question..!!!
I am waiting for it.....!!!!!
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial