Solved

JList set selected + java

Posted on 2004-03-23
7
416 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
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!

 

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

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.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

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