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.

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