JavaFX:如何在树视图中使用复选框和按钮?

时间:2015-10-27 06:04:08

标签: java javafx

我是javafx的新手。

我试图在根目录下的树表中的每一列中包含复选框,标签和按钮。

是否可以这样做。帮助我解决方案和相同的参考代码。

谢谢, Vevek。

1 个答案:

答案 0 :(得分:5)

您需要做的是为树视图设置单元格工厂。

快速回复您的评论: 只是要明确术语。树结构(在TreeView中实现)由节点组成。节点可以有多个子节点和一个父节点。没有父节点的节点是根节点。没有任何孩子的节点是叶子。每棵树只能有一个根节点。

但是你在TreeView中可以做的是隐藏根节点。如果向根节点添加多个子节点,然后隐藏根节点,则看起来您将拥有多个根节点。 TreeView中的任何节点(包括根)都是TreeItem,您可以随时通过向TreeItem添加TreeItem将该子节点添加到import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.Label; import javafx.scene.control.TreeCell; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class MCVE extends Application { public void start(Stage stage) { VBox view = new VBox(); view.setPrefSize(600, 400); // Creating the root node final TreeItem<String> root = new TreeItem<>("Root node"); root.setExpanded(true); // Creating the tree items that will be the first children of the root node // and the parent to the child nodes. final TreeItem<String> parentNode1 = new TreeItem<>("Parent node 1"); final TreeItem<String> parentNode2 = new TreeItem<>("Parent node 2"); final TreeItem<String> parentNode3 = new TreeItem<>("Parent node 3"); // Creating the tree items that will be the children of the parent // nodes. final TreeItem<String> childNode1 = new TreeItem<>("Child Node 1"); final TreeItem<String> childNode2 = new TreeItem<>("Child Node 2"); final TreeItem<String> childNode3 = new TreeItem<>("Child Node 3"); // Adding tree items to the root root.getChildren().setAll(parentNode1, parentNode2, parentNode3); // Add the child nodes to all children of the root for (TreeItem<String> parent : root.getChildren()) { parent.getChildren().addAll(childNode1, childNode2, childNode3); } // Creating a tree table view final TreeView<String> treeView = new TreeView<>(root); // We set show root to false. This will hide the root and only show it's children in the treeview. treeView.setShowRoot(false); treeView.setCellFactory(e -> new CustomCell()); view.getChildren().add(treeView); Scene scene = new Scene(view); stage.setScene(scene); stage.show(); } /** * A custom cell that shows a checkbox, label and button in the * TreeCell. */ class CustomCell extends TreeCell<String> { @Override protected void updateItem(String item, boolean empty) { super.updateItem(item, empty); // If the cell is empty we don't show anything. if (isEmpty()) { setGraphic(null); setText(null); } else { // We only show the custom cell if it is a leaf, meaning it has // no children. if (this.getTreeItem().isLeaf()) { // A custom HBox that will contain your check box, label and // button. HBox cellBox = new HBox(10); CheckBox checkBox = new CheckBox(); Label label = new Label(item); Button button = new Button("Press!"); // Here we bind the pref height of the label to the height of the checkbox. This way the label and the checkbox will have the same size. label.prefHeightProperty().bind(checkBox.heightProperty()); cellBox.getChildren().addAll(checkBox, label, button); // We set the cellBox as the graphic of the cell. setGraphic(cellBox); setText(null); } else { // If this is the root we just display the text. setGraphic(null); setText(item); } } } } public static void main(String[] args) { launch(); } } 然后可以拥有它自己的孩子等。所以你可以添加任意数量的节点。

这里是MCVE

Cell

另请参阅{{1}}课程的documentation,了解如何提供自定义单元工厂。

如果有什么不清楚,请告诉我!