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