Toggle button in menu button

379 Views Asked by At

guys,

Shortly: I would like to create MenuButton with CheckMenuItems(done). Because I would like to get rid of the check boxes and I want CheckMenuItem toggle(to change color) after they are selected I instead tryed to do it with ToggleButtons, but I am not able to put ToggleButtons/ToggleGroup inside MenuButton.

Thanks for any ideas.

1

There are 1 best solutions below

1
On

Use a RadioMenuItem.

I.e. just do

MenuButton menuButton = new MenuButton("Choices");
ToggleGroup toggleGroup = new ToggleGroup();
RadioMenuItem choice1 = new RadioMenuItem("Choice 1");
RadioMenuItem choice2 = new RadioMenuItem("Choice 2");
choice1.setToggleGroup(toggleGroup);
choice2.setToggleGroup(toggleGroup);
menuButton.getItems().setAll(choice1, choice2);

toggleGroup.selectedToggleProperty().addListener((obs, oldChoice, newChoice) -> {
    System.out.println("You chose "+((RadioMenuItem)newChoice).getText());
});