Solved

JList set selected + java

Posted on 2004-03-23
7
382 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:mayankeagle
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:mayankeagle
ID: 10669403
Is it supposed to be case-sensitive or insensitive? Which part of the selectNearest () method is it going into?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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:
mayankeagle 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now