Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Question 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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,416
    Rep Power
    20

    Default

    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

  3. #3
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    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.

  4. #4
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    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.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,416
    Rep Power
    20

    Default

    Read through all the methods. Do you see a method that sets the selected index?

    db

  6. #6
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    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!

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,416
    Rep Power
    20

    Default

    What would you suggest to use to match the typed string to the correct index?
    You already have a good approach for that in your first post.

    db

  8. #8
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    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);

    }
    }

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,416
    Rep Power
    20

    Default

    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 ;)

  10. #10
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    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)...

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,416
    Rep Power
    20

    Default

    Think about this. You already have this line of code
    Java Code:
    if (s.length() >=0) {
    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.

    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

  12. #12
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    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!!

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,416
    Rep Power
    20

    Default

    Java Code:
    setSelectedIndex(getNextMatch(...))
    db

  14. #14
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    Thanks for that, however its complaining saying that it 'cannot find symbol'. It doesn't know what this method is.

  15. #15
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Show your code, because Darryl's example is more pseudo-code than cut-n-paste code.

  16. #16
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    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));



    }
    }

  17. #17
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    getNextMatch is a method on your JList...

  18. #18
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    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.

  19. #19
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    So, is it sorted then?

  20. #20
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    Unfortunatly not, i'm still confused how to use and where to put certain methods that Darryl has said for me to use.

Page 1 of 2 12 LastLast

Similar Threads

  1. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:10 PM
  2. Link one JList to another JList
    By mib1bee in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 06:24 PM
  3. Replies: 0
    Last Post: 10-29-2009, 09:28 AM
  4. How could I search elements in a Jlist?Please Help!
    By Pro Alinio in forum New To Java
    Replies: 4
    Last Post: 03-25-2009, 11:49 PM
  5. Replies: 5
    Last Post: 08-26-2008, 03:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •