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().ge tElementAt (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.lengt h()).equal s(text)) {
//if (text.equals(item.substrin g(text.len gth()))) {
System.out.println("Coming inside 2nd if **********************");
//System.out.println("Valu e in item in if 2 is ******* "+text.charAt(0));
//System.out.println("Valu e in text in if 2 is ******* "+item.charAt(0));
list.setSelectedIndex(i);
list.updateUI();
return;
}
}
}
}
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();
item = (String)list.getModel().ge
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.lengt
//if (text.equals(item.substrin
System.out.println("Coming
//System.out.println("Valu
//System.out.println("Valu
list.setSelectedIndex(i);
list.updateUI();
return;
}
}
}
}
Is it entering the selectNearest () method? Can we have the code for the listener too?
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_WorkerNa me.getText (), list_WorkerNames);
}
public void changedUpdate(DocumentEven t e) {
}
Following is the code where i am calling this method.
//Testing DocumentListener for JTextField.
public void removeUpdate(DocumentEvent
}
public void insertUpdate(DocumentEvent
System.out.println("Value typed is ********** " +txt_WorkerName.getText())
selectNearest(txt_WorkerNa
}
public void changedUpdate(DocumentEven
}
Is it supposed to be case-sensitive or insensitive? Which part of the selectNearest () method is it going into?
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.lengt h()).equal s(text)) {
System.out.println("Coming inside 2nd if ******");
list.setSelectedIndex(i);
}
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.lengt
System.out.println("Coming
list.setSelectedIndex(i);
}
ASKER
Did you get the problem now....or not now also..................????
ASKER
Please let me know if you're in a position to answer my question..!!!
I am waiting for it.....!!!!!
I am waiting for it.....!!!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.