Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Searching code in a list

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Searching code in a list

    Hi,
    I m beginner in java coding and using netbeans 7.0 to develop a java program

    I have a searching code in jlist

    public void handleSearchByKey(String oldVal, String newVal) {
        // If the number of characters in the text box is less than last time
        // it must be because the user pressed delete
        if ( oldVal != null && (newVal.length() < oldVal.length()) ) {
            // Restore the lists original set of entries 
            // and start from the beginning
            list.setItems( entries );
        }
     
        // Change to upper case so that case is not an issue
        newVal = newVal.toUpperCase();
     
        // Filter out the entries that don't contain the entered text
        ObservableList<String> subentries = FXCollections.observableArrayList();
        for ( Object entry: list.getItems() ) {
            String entryText = (String)entry;
            if ( entryText.toUpperCase().contains(newVal) ) {
                subentries.add(entryText);
            }
        }
        list.setItems(subentries);
    }


    here is google like improvement

    public void handleSearchByKey(String oldVal, String newVal) {
        // If the number of characters in the text box is less than last time
        // it must be because the user pressed delete
        if ( oldVal != null && (newVal.length() < oldVal.length()) ) {
            // Restore the lists original set of entries 
            // and start from the beginning
            list.setItems( entries );
        }
     
        // Break out all of the parts of the search text 
        // by splitting on white space
        String[] parts = newVal.toUpperCase().split(" ");
     
        // Filter out the entries that don't contain the entered text
        ObservableList<String> subentries = FXCollections.observableArrayList();
        for ( Object entry: list.getItems() ) {
            boolean match = true;
            String entryText = (String)entry;
            for ( String part: parts ) {
                // The entry needs to contain all portions of the
                // search string *but* in any order
                if ( ! entryText.toUpperCase().contains(part) ) {
                    match = false;
                    break;
                }
            }
     
            if ( match ) {
                subentries.add(entryText);
            }
        }
        list.setItems(subentries);
    }



    WHAT I WANT IS TO RUN THIS ON PRESSING A BUTTON


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Searching code in a list

    Code, and application that uses it from drdobbs.com

Similar Threads

  1. Sorting list code
    By Nkarasjava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2012, 09:51 AM
  2. Your input with written code (Sorting and searching within array)
    By GalBenH in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 11th, 2012, 09:19 AM
  3. Sorting and Searching: Most Efficient Code
    By nicsa in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 29th, 2011, 11:20 PM
  4. Searching for string values in a circular list
    By hippo1974 in forum Object Oriented Programming
    Replies: 10
    Last Post: July 11th, 2011, 05:36 AM

Tags for this Thread