mardi 15 septembre 2020

JavaFX TreeView with CheckBoxes and RadioButtons on leafs

I'm creating an application, which has always 3 levels (4 with root) of items in TreeView and I need it to function with checkboxes and leaf items to be radioButtons.

Each level has its own TreeItem class, but all of them have common AbstractTreeItem.

Until now, I was using built-in CheckBoxTreeCell class.

TreeView example

I tried to add RadioButtons like this in TreeCellFactory but I'm missing a link between the checkboxes and radioButtons.

public class TreeCellEmulated extends CheckBoxTreeCell<String> {

  private final RadioButton radio = new RadioButton();

  @Override
  public void updateItem(String item, boolean empty) {
    super.updateItem(item, empty);

    AbstractTreeItem item = (AbstractTreeItem) getTreeItem();

    if(item != null) {
      if(item.isLeaf()) {
        if(item.getGroup() == null) {   
          ToggleGroup group = ((AbstractTreeItem) item.getParent()).getGroup();
          item.setGroup(group);
          radio.setToggleGroup(group);
          radio.selectedProperty()...updateSelecte(newValue); //My attempt to change built-in checkboxes
        }
      setGraphic(radio);
      }
    }
  }     
}   

I want to preserve the function of "external auto selection" (when you select higher level, it selects everything in lower level.) And when there is no radioButton selected in lowest level, it should select the first entry.

Lowest level TreeItem contains object which has boolean where I want to save radioButton state. (That can be probably done with selectedProprety and ChangeListener on radioButton).

Any suggestions how to improve/rework that?




Aucun commentaire:

Enregistrer un commentaire