问题
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();
}
回答1:
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);
}
How do I set the selection logic? Based on the code it gives a "CheckBox.selected : A bound value cannot be set." error.
来源:https://stackoverflow.com/questions/26488745/javafx-radio-button-checkbox-treeview