Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement filtering for treetableview

Can anyone please suggest me how to implement filtering for a TreeTableView component? I am using jdk 1.8.20

like image 720
HemantS Avatar asked Sep 27 '14 07:09

HemantS


People also ask

How do I implement the treetableview component?

The TreeTableView component has much in common with the TreeView and TableView controls: it combines and extends some aspects of their functionality. A basic implementation of a TreeTableView component in your application can be done by the following steps: Create tree items. Create the root element. Add the tree items to the root element.

What is treetableview in JavaFX?

This is a JavaFX TreeTableView Example. The TreeTableView control combines the features of the TableView and TreeView controls. It displays a TreeView inside a TableView.

How do you implement a Tree Table View in Java?

A basic implementation of a TreeTableView component in your application can be done by the following steps: Create tree items. Create the root element. Add the tree items to the root element. Create one or several columns. Define cell content. Create a tree table view. Assign columns to the tree table view.

What is the difference between a TreeView and a Tableview?

A TreeView is used to view hierarchical data. A TableView is used to view tabular data. A TreeTableView is used to view hierarchical data in a tabular form. The following table shows an overview of the whole article:


1 Answers

The example below demonstrates a TreeTableView with three columns and a filter box. Filtering is achieved by creating a new hierarchy of TreeItems. If any of child item matches all its parent items are shown as well (to preserve the tree structure).

import java.util.HashMap;
import java.util.Map;

import javafx.animation.KeyFrame;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.ListBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.CheckBox;
import javafx.scene.control.CheckBoxBuilder;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.Duration;

@SuppressWarnings("all")
public class TreeTableViewFilterDemo extends Application {

    private TreeItem<Map<String, Object>> root;
    private TreeTableView<Map<String, Object>> tree;


    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox outer = new VBox();

        TextField filter = new TextField();
        filter.textProperty().addListener((observable, oldValue, newValue) -> filterChanged(newValue));

        root = new TreeItem<>();
        tree = new TreeTableView<>(root);
        addColumn("Region", "region");
        addColumn("Type", "type");
        addColumn("Pop.", "population");
        setup();
        tree.setShowRoot(false);

        outer.getChildren().addAll(filter, tree);
        Scene scene = new Scene(outer, 640, 480);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void filterChanged(String filter) {
        if (filter.isEmpty()) {
            tree.setRoot(root);         
        }
        else {
            TreeItem<Map<String, Object>> filteredRoot = new TreeItem<>();
            filter(root, filter, filteredRoot);
            tree.setRoot(filteredRoot);
        }
    }


    private void filter(TreeItem<Map<String, Object>> root, String filter, TreeItem<Map<String, Object>> filteredRoot) {
        for (TreeItem<Map<String, Object>> child : root.getChildren()) {            
            TreeItem<Map<String, Object>> filteredChild = new TreeItem<>();
            filteredChild.setValue(child.getValue());
            filteredChild.setExpanded(true);
            filter(child, filter, filteredChild );
            if (!filteredChild.getChildren().isEmpty() || isMatch(filteredChild.getValue(), filter)) {
                System.out.println(filteredChild.getValue() + " matches.");
                filteredRoot.getChildren().add(filteredChild);
            }
        }
    }

    private boolean isMatch(Map<String, Object> value, String filter) {
        return value.values().stream().anyMatch((Object o) -> o.toString().contains(filter));
    }

    private void setup() {
        TreeItem<Map<String, Object>> europe = createItem(root, "Europe", "continent", 742500000L);
        createItem(europe, "Germany", "country", 80620000L);
        TreeItem<Map<String, Object>> austria = createItem(europe, "Austria", "country", 847400L);
        createItem(austria, "Tyrol", "state", 728537L);     
        TreeItem<Map<String, Object>> america = createItem(root, "America", "continent", 953700000L);
        createItem(america, "USA", "country", 318900000L);
        createItem(america, "Mexico", "country", 122300000L);       
    }

    private TreeItem<Map<String, Object>> createItem(TreeItem<Map<String, Object>> parent, String region, String type, long population) {
        TreeItem<Map<String, Object>> item = new TreeItem<>();
        Map<String, Object> value = new HashMap<>();
        value.put("region",  region);
        value.put("type", type);
        value.put("population", population);
        item.setValue(value);
        parent.getChildren().add(item);
        item.setExpanded(true);
        return item;
    }

    protected void addColumn(String label, String dataIndex) {
        TreeTableColumn<Map<String, Object>, String> column = new TreeTableColumn<>(label);
        column.setPrefWidth(150);
        column.setCellValueFactory(
            (TreeTableColumn.CellDataFeatures<Map<String, Object>, String> param) -> {
                ObservableValue<String> result = new ReadOnlyStringWrapper("");
                if (param.getValue().getValue() != null) {
                    result = new ReadOnlyStringWrapper("" + param.getValue().getValue().get(dataIndex));
                }
                return result;
            }
        );      
        tree.getColumns().add(column);
    }


    public static void main(String[] args) {
        launch(args);
    }

}
like image 55
rli Avatar answered Nov 24 '22 22:11

rli