2

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.

4
  • Can you give us some more data on the table ? As per your current question, it can achieved easily from a SQL query ! Commented Mar 4, 2014 at 20:54
  • @ItachiUchiha Ive added a picture of the table Commented Mar 4, 2014 at 21:20
  • This is your current display, you want to filter data based on ORDER STATUS ? Commented Mar 4, 2014 at 21:23
  • yeah thats what i would like to do :) Commented Mar 4, 2014 at 21:24

2 Answers 2

3

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())
);
Sign up to request clarification or add additional context in comments.

3 Comments

I wasn't that interested in using predicate binding because I was worried about the performance hit on every key press. I just tried it with regex matching on 25000 lines and it seems instantaneous. A quick look at the cpus shows less use than just mousing over a javafx stage.
The way it works (I believe) is that on every key press, the predicate property is changed which simply results in the items property being marked as invalid. Then on each frame render that observes an invalid property, it will recompute the filtered list and the rendered cells. So no matter how fast you type, the regexp matches are only performed at most once per frame, i.e. 60 times per second, which is more than ample time for most systems to compute 25K regexps.
I have a problem with binding to a regexp, no way to trap the error from a half-typed expression :(
2

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 !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.