I would like to display only certain information in table view such as only \"male\" people in a database. I\'m only at using javafx. Thanks for your help in advance.
<If you are using Javafx 2.0+, you have to write your custom table view filter or you can use javafx_filterable_columns. For custom table filters, you can follow this link, which provides a very nice approach to writing filters
http://code.makery.ch/blog/javafx-8-tableview-sorting-filtering/
In Javafx8, may be the facility is inbuilt. But, I am not sure, since I have never used it personally !
If you can use java 8 you can use the built in FilteredList and predicates. Here's something I wrote to test regex filtering. I modified it a bit to be more like your example and use javafx 2.2 if required. Just change some of the commented lines to use 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, there's supposedly a way to bind the predicateProperty, but I can't figure that out.
Edit: If you want a binding, instead of the handler for ActionEvent, do something like
filteredItems.predicateProperty().bind(
Bindings.createObjectBinding(() ->
li -> li.desc.getValue().contains(filterText.getText()),
filterText.textProperty())
);