Jika Anda dapat menggunakan Java 8, Anda dapat menggunakan FilteredList dan predikat bawaan. Inilah sesuatu yang saya tulis untuk menguji pemfilteran regex. Saya memodifikasinya sedikit agar lebih seperti contoh Anda dan menggunakan javafx 2.2 jika diperlukan. Ubah saja beberapa baris komentar untuk menggunakan Java 8.
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TableTest extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<LineItem> items = FXCollections.observableArrayList();
items.addAll(new LineItem("hello",123.45),
new LineItem("paid in full",0.01),
new LineItem("paid",0.01),
new LineItem("due",0.01),
new LineItem("paid",0.01));
//for java8
//FilteredList<LineItem> filteredItems = new FilteredList(items, e->true);
//not java8
ObservableList<LineItem> filteredItems = FXCollections.observableArrayList(items);
TableView tableView = new TableView(filteredItems);
TableColumn<LineItem,String> descCol = new TableColumn<>("desc");
descCol.setCellValueFactory(new PropertyValueFactory<>("desc"));
TableColumn<LineItem, Double> amountCol = new TableColumn<>("amount");
amountCol.setCellValueFactory(new PropertyValueFactory<>("amount"));
tableView.getColumns().addAll(descCol,amountCol);
TextField filterText = new TextField();
filterText.setPromptText("type filter and press enter");
filterText.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
//normal java8
//filteredItems.setPredicate(li -> li.desc.getValue().contains(filterText.getText()));
//regex java 8
//filteredItems.setPredicate(li -> li.desc.getValue().matches("(?i)"+filterText.getText()));
//not javafx 8
filteredItems.clear();
for (LineItem li: items)
if (li.desc.getValue().contains(filterText.getText()))
filteredItems.add(li);
}
});
VBox root = new VBox();
root.getChildren().addAll(tableView, filterText);
Scene scene = new Scene(root, 300, 300);
primaryStage.setTitle("Filter table test");
primaryStage.setScene(scene);
primaryStage.show();
}
public class LineItem {
private final StringProperty desc = new SimpleStringProperty();
private final DoubleProperty amount = new SimpleDoubleProperty();
public StringProperty descProperty() {return desc;}
public DoubleProperty amountProperty() {return amount;}
public LineItem(String dsc, double amt) {
desc.set(dsc); amount.set(amt);
}
}
}
todo, seharusnya ada cara untuk mengikat predikatProperty, tapi saya tidak tahu.
Sunting:Jika Anda menginginkan penjilidan, alih-alih handler untuk ActionEvent, lakukan sesuatu seperti
filteredItems.predicateProperty().bind(
Bindings.createObjectBinding(() ->
li -> li.desc.getValue().contains(filterText.getText()),
filterText.textProperty())
);