Source Code : ListView auto filter
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
ListView auto filter
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
ObservableList<String> entries = FXCollections.observableArrayList();
ListView list = new ListView();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Simple Search");
TextField txt = new TextField();
txt.setPromptText("Search");
txt.textProperty().addListener(new ChangeListener() {
public void changed(ObservableValue observable, Object oldVal,
Object newVal) {
search((String) oldVal, (String) newVal);
}
});
list.setMaxHeight(180);
for (int i = 0; i < 100; i++) {
entries.add("Item " + i);
}
entries.add("A");
entries.add("B");
entries.add("C");
entries.add("D");
list.setItems(entries);
VBox root = new VBox();
root.setPadding(new Insets(10, 10, 10, 10));
root.setSpacing(2);
root.getChildren().addAll(txt, list);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public void search(String oldVal, String newVal) {
if (oldVal != null && (newVal.length() < oldVal.length())) {
list.setItems(entries);
}
String value = newVal.toUpperCase();
ObservableList<String> subentries = FXCollections.observableArrayList();
for (Object entry : list.getItems()) {
boolean match = true;
String entryText = (String) entry;
if (!entryText.toUpperCase().contains(value)) {
match = false;
break;
}
if (match) {
subentries.add(entryText);
}
}
list.setItems(subentries);
}
}
Thank with us