Treeview JavaFX:使用单选按钮创建树视图

Treeview JavaFX:使用单选按钮创建树视图,treeview,javafx,radiobuttonlist,Treeview,Javafx,Radiobuttonlist,用例:我试图提供一种功能,用户可以将元素组装/组合成最终的解决方案。元素将有不同的版本。为此,我需要一组复选框来定义要包含哪些元素,然后单选按钮(嵌套在每个复选框下)来定义所选元素应使用的版本 我目前正在使用ControlsFX中的CheckTreeView控件。但是我找不到一种方法将RadioButtonMemoItems作为CheckBoxTreeItem的子项放在树中。有没有办法把CheckBoxTreeItem改成单选按钮 我目前的解决方案是,我对所有树节点都使用CheckBoxItem

用例:我试图提供一种功能,用户可以将元素组装/组合成最终的解决方案。元素将有不同的版本。为此,我需要一组复选框来定义要包含哪些元素,然后单选按钮(嵌套在每个复选框下)来定义所选元素应使用的版本

我目前正在使用ControlsFX中的CheckTreeView控件。但是我找不到一种方法将RadioButtonMemoItems作为CheckBoxTreeItem的子项放在树中。有没有办法把CheckBoxTreeItem改成单选按钮

我目前的解决方案是,我对所有树节点都使用CheckBoxItems,但是那些用于定义版本的项就像单选按钮一样——选择一个将取消选择其余的

关于如何处理这个问题有什么想法吗


编辑:在此处发布新问题+代码

对于初学者,您需要创建自己的自定义TreeCellFactory,根据需要显示复选框或单选按钮。比如:

public class TreeCellFactory implements Callback<TreeView<Object>,TreeCell<Object>>
{
    @Override
    public TreeCell call( TreeView param )
    {
        return new TreeCell<Object>()
        {
            private final CheckBox  check = new CheckBox();
            private final RadioButton  radio = new RadioButton();
            private Property<Boolean>  prevRadioProp;
            {
                setContentDisplay( ContentDisplay.GRAPHIC_ONLY );
            }

            @Override
            public void updateItem( Object item, boolean empty )
            {
                if ( prevRadioProp != null )
                {
                    radio.selectedProperty().unbindBidirectional( prevRadioProp );
                    prevRadioProp = null;
                }
                check.selectedProperty().unbind();

                if ( ! empty && item != null )
                {
                    Property<Boolean> selectedProp = ....;

                    if ( getTreeItem().isLeaf() )  // display radio button
                    {
                        radio.setText( ... );
                        radio.selectedProperty().bindBidirectional( selectedProp );
                        prevRadioProp = selectedProp;
                        setGraphic( radio );
                    }
                    else                          // display checkbox
                    {
                        check.setText( ... );
                        check.selectedProperty().bind( selectedProp );
                        setGraphic( check );
                    }
                }
                else
                {
                    setGraphic( null );
                    setText( null );
                }
            }
        };
    }
}
公共类TreeCellFactory实现回调
{
@凌驾
公共TreeCell调用(TreeView参数)
{
返回新的TreeCell()
{
私有最终复选框check=新复选框();
专用最终单选按钮=新单选按钮();
私人财产;
{
setContentDisplay(仅限ContentDisplay.GRAPHIC_);
}
@凌驾
public void updateItem(对象项,布尔值为空)
{
如果(prevRadioProp!=null)
{
radio.selectedProperty().UnbindBidirective(prevRadioProp);
prevRadioProp=null;
}
check.selectedProperty().unbind();
如果(!empty&&item!=null)
{
属性selectedProp=。。。。;
if(getTreeItem().isLeaf())//显示单选按钮
{
radio.setText(…);
radio.selectedProperty().binddiregical(selectedProp);
prevRadioProp=selectedProp;
设置图形(无线电);
}
else//显示复选框
{
check.setText(…);
选中.selectedProperty().bind(selectedProp);
设置图形(检查);
}
}
其他的
{
设置图形(空);
setText(空);
}
}
};
}
}

谢谢,我来试一试。