JavaFX:单选按钮 + CheckBox TreeView [英] JavaFX: Radio Button + CheckBox TreeView
问题描述
参考我之前的问题这里,我做了下面的代码.
in reference to my earlier question here, I did the code below.
我正在尝试制作一个树形视图,其中显示叶子的单选按钮和非叶子项目的复选框.下面的代码没有显示任何内容.我确信我在某处(或任何地方)做的非常错误.任何帮助深表感谢.谢谢
I am trying to make a treeview which shows a radio button for leafs, and checkboxes for non-leaf items. The code below does not show anything. I am sure I am doing something extremely wrong somewhere (or everywhere). Any help is much appreciated. Thanks
public class RadioCheckBoxTreeView extends TreeView {
public RadioCheckBoxTreeView() {
setCellFactory(new Callback<TreeView<Object>, TreeCell<Object>>() {
@Override
public TreeCell<Object> call(TreeView<Object> param) {
return new RadioCheckBoxCellImpl();
}
});
}
private static class RadioCheckBoxCellImpl extends TreeCell<Object> {
private final CheckBox check = new CheckBox();
private final RadioButton radio = new RadioButton();
private Property<Boolean> prevRadioProp;
public RadioCheckBoxCellImpl() {
}
{
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 = prevRadioProp;
if (getTreeItem().isLeaf()) // display radio button
{
radio.setText("radio");
radio.selectedProperty().bindBidirectional(selectedProp);
prevRadioProp = selectedProp;
setGraphic(radio);
} else // display checkbox
{
check.setText("check");
check.selectedProperty().bind(selectedProp);
setGraphic(check);
}
} else {
setGraphic(null);
setText(null);
}
}
}
这就是我的启动方法的样子
this is what my start method looks like
public void start(Stage primaryStage) {
AnchorPane pane = new AnchorPane();
Scene scene = new Scene(pane);
MyTreeView tv = new MyTreeView();
tv.setRoot(new TreeItem());
TreeItem child1 = new TreeItem();
child1.setValue("1");
TreeItem child2 = new TreeItem();
child2.setValue("2");
TreeItem child3 = new TreeItem();
child3.setValue("3");
tv.getRoot().getChildren().add(child1);
tv.getRoot().getChildren().add(child2);
child2.getChildren().add(child3);
pane.getChildren().add(tv);
primaryStage.setScene(scene);
primaryStage.show();
}
推荐答案
好的,我设法做到了这一点.我还更改了图形以根据其类型进行更改.
Ok I managed to get this far. I also changed the graphic to change based on its type.
@Override
public void updateItem(Object item, boolean empty) {
if (prevRadioProp != null) {
radio.selectedProperty().unbindBidirectional(prevRadioProp);
prevRadioProp.setValue(true);
}
check.selectedProperty().unbind();
if (!empty && item != null) {
SimpleBooleanProperty selectedProp = prevRadioProp;
if (item.getClass().getName().contains("Option")) // display radio button
{
radio.setText("Option");
radio.selectedProperty().bindBidirectional(selectedProp);
prevRadioProp = selectedProp;
setGraphic(radio);
} else // display checkbox
{
check.setText("Feature");
check.selectedProperty().bind(selectedProp);
setGraphic(check);
}
} else {
setGraphic(null);
setText(null);
}
super.updateItem(item, empty);
}
如何设置选择逻辑?根据代码,它给出了CheckBox.selected:无法设置绑定值".错误.
How do I set the selection logic? Based on the code it gives a "CheckBox.selected : A bound value cannot be set." error.
这篇关于JavaFX:单选按钮 + CheckBox TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!