【问题标题】:How to display an object's properties as controls in a single cell item of a TreeView in JavaFx?如何在 JavaFx 中的 TreeView 的单个单元格项中将对象的属性显示为控件?
【发布时间】:2017-05-10 12:49:42
【问题描述】:

是否可以在 TreeView 的单元格项中通过标签、组合框(或更多控件)来表示节点的属性? 我想在每个树形视图单元格中显示节点的名称、选定的项目(以及更多控件(例如复选框),如果可能的话)。 可以这样做吗?

根据 Oracle 的教程 (http://docs.oracle.com/javafx/2/ui_controls/tree-view.htm),CheckBoxTreeCell、ChoiceBoxTreeCell、ComboBoxTreeCell、TextFieldTreeCell 类可用于类似目的,但我不知道如何在单个单元格中使用其中的许多类,或者如何制作单元格的可编辑模板。

我的模型是这样的:

public enum Options {
    Option1, Option2, ... OptionN;
}

// I want to use this Node class as a TreeItem
public class Node {

    private Node parentNode;
    private List<Node> childNodes;

    // Data to be displayed and edited by using only the tree
    private String name;            // displayed as Label
    private Options selectedOption; // displayed as ComboBox
    // private boolean valid;       // displayed as Checkbox
    // these properties possibly should be JavaFX's ObjectProperty, StringProperty, ListProperty etc.

    // ...
}

我想显示的内容类似于:

Node0 [ComboBox: (selectedOption = Options.OptionI)]
|-- Node1 [ComboBox: (selectedOption = Options.OptionJ)]
|   |-- Node11 [ComboBox: (selectedOption = Options.OptionK)]
|-- Node2 [ComboBox: (selectedOption = Options.OptionK)]
    |-- Node21 [ComboBox: (selectedOption = Options.OptionL)]
    |-- Node22 [ComboBox: (selectedOption = Options.OptionJ)]

... 用户可以通过编辑树的元素来设置节点的属性。 我应该使用什么方法来实现此功能?

对不起,如果这是一个基本问题,我刚刚开始学习 JavaFx。

【问题讨论】:

标签: javafx data-binding combobox treeview controls


【解决方案1】:

您可能需要考虑使用TreeTableView。如果 Control 不适合您的需求,您可以使用自定义 TreeCell

TreeView<Node> treeView = ...
treeView.setCellFactory(t -> new TreeCell<Node>() {

    private final ComboBox<Options> comboBox = new ComboBox<>(FXCollections.observableArrayList(Options.values()));
    private final CheckBox valid = new CheckBox("valid");
    private final VBox container = new VBox(comboBox, valid); // todo: add more controls for other properties

    private boolean swapping;

    {
        comboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (!swapping) {
                 getItem().setSelectedOption(newValue);
            }
        });
        valid.selectedProperty().addListener((observable, oldValue, newValue) -> {
            if (!swapping) {
                 getItem().setValid(newValue);
            }
        });

        // todo: listeners to other controls...

        setContentDisplay(ContentDisplay.BOTTOM);
    }

    @Override
    public void updateItem(Node item, boolean empty) {
        super.updateItem();
        if (empty) {
            setText("");
            setGraphic(null);
        } else {
            setText(item.getName());
            setGraphic(container);
            swapping = true;

            comboBox.setValue(item.getSelectedOption());
            valid.setSelected(item.isValid());
            // todo: initialize other nodes with item values

            swapping = false;
        }
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-30
    • 2014-05-21
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多