Lycon Lycon -4 years ago 148
MySQL Question

Filtering data to display from database in javafx

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.

This is my current table
enter image description here

I would like to filter the table so that only rows with "order status : PAID" are displayed in the table.

Answer Source

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.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 {

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


        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
                for (LineItem li: items)
                    if (li.desc.getValue().contains(filterText.getText()))

        VBox root = new VBox();
        root.getChildren().addAll(tableView, filterText);
        Scene scene = new Scene(root, 300, 300);

        primaryStage.setTitle("Filter table test");

    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

        Bindings.createObjectBinding(() -> 
                li -> li.desc.getValue().contains(filterText.getText()), 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download