Results 1 to 20 of 24
Thread: Search within a JList
- 03-22-2011, 10:42 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
Search within a JList
For my assignment, I was asked if I could implement a search textbox so that when someone types say a name in the textbox, it highlights the name in the JList or at least tells you that it exists. I may have to implement a search button as well. I'm not sure how to go about doing this, however I have found a method called getNextMatch which supposedly allows you to search within a JList but I have no idea how to apply it. If anyone has any ideas or any suggestions it would be really helpful.
- 03-22-2011, 11:00 AM #2
Here's a few hints:
What type of value is returned from JList#getNextMatch?
What is the significance of the returned value?
Do you see a JList method that selects a single cell?
db
- 03-22-2011, 11:14 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
The value returned is an Int. The definition from Oracle is 'Returns the data model that holds the list of items displayed by the JList component'. I hope this will do what I want it to do.
The significance of the returned value is one of the data items in the JList I am assuming.
I have this: list.setSelectionMode(ListSelectionModel.SINGLE_SE LECTION);
I'm not sure on a particular method exactly though. Btw, I am not a very experienced programmer and I don't find it very easy! However I don't give up easy either.
- 03-22-2011, 11:38 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
I've thought of another way of doing it. Is it possible to compare an item in a list to a typed string using the compareTo method? If thats possible then it could work this way.
- 03-22-2011, 01:17 PM #5
Read through all the methods. Do you see a method that sets the selected index?
db
- 03-22-2011, 03:21 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
Yes I do see a method called setSelectedIndex(). Right thats half working! Got the setSelectedIndex at 0 and when I type a search and press the search button its going straight to the first index. I want it to match the String value. However its a really helpful start! Thanks. What would you suggest to use to match the typed string to the correct index? Many thanks for all our help!
- 03-22-2011, 04:27 PM #7
You already have a good approach for that in your first post.What would you suggest to use to match the typed string to the correct index?
db
- 03-23-2011, 10:33 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
Right well its just figuring out how to apply getNextMatch() to list.setSelectedIndex() . At the moment i've got:
String s = search.getText(); //search = JTextfield with a JButton attached
if (s.length() >=0) {
list.getNextMatch(s, 0, Bias.Forward);
list.setSelectedIndex(0);
}
}
- 03-23-2011, 11:06 AM #9
getNextMatch(...) returns a value. I don't see you using that returned value.
db
edit You're very close to attaining a solution. That ---><--- close ;)
- 03-23-2011, 12:36 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
Right thats good news! It says that it Returns the index of the next list element that starts with the prefix. Which is exactly what I want. So I need something like:
list.getNextMatch(s, 0, Bias.Forward);
return list. i'm suck after here! (I know its the index of 's' which is the prefix)...
- 03-23-2011, 01:21 PM #11
Think about this. You already have this line of code
The length() method of class String is also a method that returns int. In that line of code, you are comparing the returned value to 0. You probably already have other lines of code that invoke a method and use the value returned in some way.Java Code:if (s.length() >=0) {
If you put s.length(); on a line all by itself, the method is called but the returned value is discarded. And that's what you're doing with getNextMatch(...).
Assign the returned value to a variable and use that variable to set the selection and you're good to go.
db
- 03-24-2011, 11:09 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
Right, I understand what you are saying, however I am still unfortunately confused. I am doing the right thing with the 'if' statement? And I know I am not returning a value with getNextMatch but I am unsure how to do this. Also is the returned value 's'? and you apply the returned value to a variable to set, setSelectedIndex();. I am also unsure how to assign a returned value to a variable. I am awfully frustrated with myself as I should be able to do this!!
- 03-24-2011, 11:22 AM #13
dbJava Code:setSelectedIndex(getNextMatch(...))
- 03-24-2011, 11:27 AM #14
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
Thanks for that, however its complaining saying that it 'cannot find symbol'. It doesn't know what this method is.
- 03-24-2011, 11:41 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Show your code, because Darryl's example is more pseudo-code than cut-n-paste code.
- 03-24-2011, 11:47 AM #16
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
ok, this is what i've got:
Searchbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = search.getText();
if (s.length() <=0) {
JOptionPane.showMessageDialog(Searchbtn,"No search query entered! ", "No query",
JOptionPane.WARNING_MESSAGE);
}
if (s.length() >=0) {
list.setSelectedIndex(getNextMatch (s, 0, Bias.Forward));
}
}
- 03-24-2011, 11:48 AM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
getNextMatch is a method on your JList...
- 03-24-2011, 12:02 PM #18
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
Yes, it is. Thats what I need to use to return a value to search for an item using a string in the JList. Darryl was a great help in providing me with some points to help with this.
- 03-24-2011, 12:13 PM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
So, is it sorted then?
- 03-24-2011, 12:16 PM #20
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Link one JList to another JList
By mib1bee in forum AWT / SwingReplies: 1Last Post: 12-31-2010, 07:10 PM -
Link one JList to another JList
By mib1bee in forum New To JavaReplies: 1Last Post: 12-30-2010, 06:24 PM -
Not able to highlight the search key work in wild card search
By annappa in forum LuceneReplies: 0Last Post: 10-29-2009, 09:28 AM -
How could I search elements in a Jlist?Please Help!
By Pro Alinio in forum New To JavaReplies: 4Last Post: 03-25-2009, 11:49 PM -
make search function ike eclipse search in window->preference
By i4ba1 in forum Advanced JavaReplies: 5Last Post: 08-26-2008, 03:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks