Forum Stats

  • 3,826,776 Users
  • 2,260,707 Discussions
  • 7,897,072 Comments

Discussions

[JavaFX] TreeTableView no renderer for non-leaf nodes

TPD-Opitz
TPD-Opitz Member Posts: 2,465 Silver Trophy
edited Feb 15, 2016 8:41AM in Java 8 Questions

Hello,

I'd like to implement a TreeTableWiew with special Renderers (CheckBoxTreeTableCell in the SSCCE)

But in rows the of non leaf nodes I don't want the specialized renderers to be shown.

Can someone please change my example so that the CheckBox is not shown in the first row?

/*
 * //from www.java2s.com Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved. Use is
 * subject to license terms. This file is available and licensed under the following license: Redistribution
 * and use in source and binary forms, with or without modification, are permitted provided that the following
 * conditions are met: - Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer. - Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer in the documentation and/or other
 * materials provided with the distribution. - Neither the name of Oracle nor the names of its contributors
 * may be used to endorse or promote products derived from this software without specific prior written
 * permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */
import java.util.Arrays;
import java.util.List;

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.CheckBoxTreeTableCell;
import javafx.stage.Stage;

public class FxMain extends Application {

    List<Employee> employees =
            Arrays.<Employee> asList(new Employee("Ethan Williams", "[email protected]", false),
                    new Employee("Emma Jones", "[email protected]", false),
                    new Employee("Michael Brown", "[email protected]", true),
                    new Employee("Anna Black", "[email protected]", true),
                    new Employee("Rodger York", "[email protected]", false),
                    new Employee("Susan Collins", "[email protected]", true));

    final TreeItem<Employee> root = new TreeItem<>(new Employee("Sales Department", "", false));

    public static void main(String[] args) {
        Application.launch(FxMain.class, args);
    }

    @Override
    public void start(Stage stage) {
        root.setExpanded(true);
        employees.stream().forEach((employee) -> {
            root.getChildren().add(new TreeItem<>(employee));
        });
        Scene scene = new Scene(new Group(), 400, 400);
        Group sceneRoot = (Group) scene.getRoot();

        TreeTableColumn<Employee, String> empColumn = new TreeTableColumn<>("Employee");
        empColumn.setPrefWidth(150);
        empColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<Employee, String> param) -> param.getValue()
                .getValue()
                .nameProperty());

        TreeTableColumn<Employee, String> emailColumn = new TreeTableColumn<>("Email");
        emailColumn.setPrefWidth(190);
        emailColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<Employee, String> param) -> param.getValue()
                .getValue()
                .emailProperty());

        TreeTableColumn<Employee, Boolean> superiorColumn = new TreeTableColumn<>("is Superior");
        superiorColumn.setPrefWidth(190);
        superiorColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<Employee, Boolean> param) -> {
            Employee employee = param.getValue().getValue();
            return employee.isSuperiorProperty();
        });
        superiorColumn.setCellFactory(col -> {
            // what to change here to get no checkbox for department entry??
            CheckBoxTreeTableCell<Employee, Boolean> checkBoxTreeTableCell = new CheckBoxTreeTableCell<>()
            checkBoxTreeTableCell.setEditable(false);
            return checkBoxTreeTableCell;
        });

        TreeTableView<Employee> treeTableView = new TreeTableView<>(root);
        treeTableView.setEditable(true);
        treeTableView.getColumns().setAll(empColumn, emailColumn, superiorColumn);
        sceneRoot.getChildren().add(treeTableView);
        stage.setScene(scene);
        stage.show();
    }

    public class Employee {

        private final SimpleStringProperty name;
        private final SimpleStringProperty email;
        private final BooleanProperty isSuperior;

        public Boolean getIsSuperior() {
            return isSuperior.get();
        }

        public void setIsSuperior(Boolean isSuperior) {
            this.isSuperior.set(isSuperior);
        }

        public SimpleStringProperty nameProperty() {
            return name;
        }

        public BooleanProperty isSuperiorProperty() {
            return isSuperior;
        }

        public SimpleStringProperty emailProperty() {
            return email;
        }

        private Employee(String name, String email, Boolean isSuperior) {
            this.name = new SimpleStringProperty(name);
            this.email = new SimpleStringProperty(email);
            this.isSuperior = new SimpleBooleanProperty(isSuperior);
        }

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

    }
}

Thanks

TPD

Tagged: