JavaFX:使用单选按钮创建树视图 [英] JavaFX: Making a Tree View with Radio Buttons

查看:214
本文介绍了JavaFX:使用单选按钮创建树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用例:我正在尝试提供一种功能,用户将元素组合/组合成最终解决方案。元素会有版本。为此,我需要CheckBoxes的组合来定义要包含的元素,然后单击Radio Buttons(嵌套在每个复选框下)以定义应该为所选元素使用的版本。

Use case: I am trying to offer a functionality where the user assembles/combines elements into a final solution. Elements would have versions. To do that, I need a combination of CheckBoxes to define which elements to include, and then Radio Buttons (nested under each check box) to define what version should be used for the selected element.

我目前正在使用来自ControlsFX的CheckTreeView控件。但我找不到一种方法将RadioButtonMenuItems作为CheckBoxTreeItem的子项放在树中。有没有办法将CheckBoxTreeItem更改为RadioButton?

I am currently using a CheckTreeView control, from ControlsFX. but I can't find a way to put RadioButtonMenuItems as children for CheckBoxTreeItem in the tree. Is there a way to change the CheckBoxTreeItem to look like a RadioButton?

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

My current solution is that I am using CheckBoxItems for all tree nodes, but those that are being used for defining the version act like Radio buttons--selecting one would unselect the rest.

有关如何处理此问题的任何想法?

Any ideas on how to approach this?

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

posted new question + code here here

推荐答案

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

For starters you'll need to create your own custom TreeCellFactory that'll display a checkbox or a radiobutton as needed. Something like:

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

这篇关于JavaFX:使用单选按钮创建树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆