过滤数据以在javafx中显示数据库 [英] Filtering data to display from database in javafx

查看:180
本文介绍了过滤数据以在javafx中显示数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表格视图中仅显示某些信息,例如数据库中仅显示男性人物。我只是在使用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

我想过滤表格,以便只有表格中显示订单状态:PAID的行。

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

推荐答案

如果你可以使用java 8,你可以使用内置的FilteredList和谓词。这是我为测试正则表达式过滤而编写的内容。我修改了一下,更像是你的例子,如果需要,可以使用javafx 2.2。只需更改一些注释行即可使用java 8.

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,有一种方法可以绑定predicateProperty,但我无法弄明白。

todo, there's supposedly a way to bind the predicateProperty, but I can't figure that out.

编辑:如果你想要一个绑定,而不是ActionEvent的处理程序,做一些事情喜欢

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

这篇关于过滤数据以在javafx中显示数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆