当前位置:   article > 正文

treeTabelView --带checkBox可选择的树状表格_javafx 自定义checkboxtreecell

javafx 自定义checkboxtreecell

在这里插入图片描述


import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.CheckBoxTreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.CheckBoxTreeTableCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.util.Arrays;
import java.util.List;

public class MySelfTreeView extends Application {

    public static void main(String[] args) {

        launch(args);

    }

    CheckBoxTreeItem<Employee> rootItem = null;
    TreeTableView<Employee> tree = null;

    /**
     * @param primaryStage
     * @throws Exception
     */
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Tree View Sample");
        List<Employee> employees = Arrays.asList(new Employee("Emma Jones", "emma.jones@example.com"),
                new Employee("Michael Brown", "michael.brown@example.com"),
                new Employee("Anna Black", "anna.black@example.com"),
                new Employee("Rodger York", "roger.york@example.com"),
                new Employee("Susan Collins", "susan.collins@example.com"));
        TreeTableColumn<Employee, String> empColumn = new TreeTableColumn<>("Employee");
        empColumn.setPrefWidth(150);
        empColumn.setCellValueFactory(
                (TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper(
                        param.getValue().getValue().getName()));

        TreeTableColumn<Employee, String> emailColumn = new TreeTableColumn<>("email");
        emailColumn.setPrefWidth(190);
        emailColumn.setCellValueFactory(
                (TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper(
                        param.getValue().getValue().getEmail()));
      tree =new TreeTableView<>();
        tree.setEditable(true);

        rootItem = new CheckBoxTreeItem<>(new Employee("Ethan Williams", ""));
        rootItem.setExpanded(true);
        for (Employee employee : employees) {
            final CheckBoxTreeItem<Employee> checkBoxTreeItem = new CheckBoxTreeItem<>(employee);
            rootItem.getChildren().add(checkBoxTreeItem);
            checkBoxTreeItem.getChildren().add(new CheckBoxTreeItem<>(employee));
        }
        tree.setRoot(rootItem);

        tree.setShowRoot(true);
        TreeTableColumn<Employee, Boolean> empColumn2 = new TreeTableColumn<>();
        CheckBoxTreeTableCell checkCell = new CheckBoxTreeTableCell();
        empColumn2.setPrefWidth(100);
        empColumn2.setCellValueFactory(
                (TreeTableColumn.CellDataFeatures<Employee, Boolean> param) -> new ReadOnlyBooleanWrapper(
                        param.getValue().getValue().getSelectBoolean()));
        empColumn2.setCellFactory(checkCell.forTreeTableColumn(getSelectedProperty));
        rootItem.setIndependent(false);

        checkCell.setSelectedStateCallback(new Callback<Integer, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(Integer param) {
                System.out.println("选中事件-->" + param);
                return null;
            }
        });


        tree.getColumns().setAll(empColumn2, empColumn, emailColumn);
        StackPane root = new StackPane();

        root.getChildren().add(tree);

        primaryStage.setScene(new Scene(root, 300, 250));

        primaryStage.show();
    }


    Callback<Integer, ObservableValue<Boolean>> getSelectedProperty = new Callback<Integer, ObservableValue<Boolean>>() {
        @Override
        public ObservableValue<Boolean> call(Integer param) {
            return ((CheckBoxTreeItem<?>) tree.getTreeItem(param)).selectedProperty();
        }
    };
    class Employee {
        private BooleanProperty  selectBoolean;
        private SimpleStringProperty name;
        private SimpleStringProperty email;
        public SimpleStringProperty nameProperty() {
            if (name == null) {
                name = new SimpleStringProperty(this, "name");
            }
            return name;
        }
        public SimpleStringProperty emailProperty() {
            if (email == null) {
                email = new SimpleStringProperty(this, "email");
            }
            return email;
        }
        public BooleanProperty selectBooleanProperty() {
            if (selectBoolean == null) {
                selectBoolean = new SimpleBooleanProperty(this, "selectBoolean");
            }
            return selectBoolean;
        }
        public Employee(String name, String email) {
            this.name = new SimpleStringProperty(name);
            this.email = new SimpleStringProperty(email);
            this.selectBoolean = new SimpleBooleanProperty(true);
        }
        public String getName() {
            return name.get();
        }
        public void setName(String fName) {
            name.set(fName);
        }
        public String getEmail() {
            return email.get();
        }
        public void setEmail(String fName) {
            email.set(fName);
        }
        public Boolean getSelectBoolean() {
            return selectBoolean.get();
        }
        public void setSelectBoolean(Boolean select) {
            this.selectBoolean.set(select);
        }
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/191663
推荐阅读
相关标签
  

闽ICP备14008679号