JavaFX: Making a Tree View with Radio Buttons

后端 未结 1 1784
星月不相逢
星月不相逢 2020-12-22 05:05

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 combinati

1条回答
  •  独厮守ぢ
    2020-12-22 05:49

    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,TreeCell>
    {
        @Override
        public TreeCell call( TreeView param )
        {
            return new TreeCell()
            {
                private final CheckBox  check = new CheckBox();
                private final RadioButton  radio = new RadioButton();
                private Property  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 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 );
                    }
                }
            };
        }
    }
    
        

    0 讨论(0)
    提交回复
    热议问题