Solved

JList set selected + java

Posted on 2004-03-23
7
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

726 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