Skip to content

Instantly share code, notes, and snippets.

Created May 3, 2017 17:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
JavaFX: Búsqueda en un ListView con FilteredList
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
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 {
public static void main(String[] args) {
public void start(Stage primaryStage) throws Exception {
ObservableList<String> items = FXCollections.observableArrayList("Juan", "Maria", "Pedro", "Mariela", "Juan Pablo");
FilteredList<String> datosFiltradosLista = new FilteredList<String>(items, s -> true);
TextField busquedaTextField = new TextField();
busquedaTextField.textProperty().addListener(new ChangeListener<String>() {
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
String texto = busquedaTextField.getText();
if (texto == null || texto.length() == 0) {
datosFiltradosLista.setPredicate(s -> true);
} else {
datosFiltradosLista.setPredicate(s -> s.contains(texto));
ListView<String> listView = new ListView<String>(datosFiltradosLista);
VBox vBox = new VBox();
vBox.setPadding(new Insets(20, 20, 20, 20));
Scene scene = new Scene(vBox, 400, 400);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment