切换菜单按钮中的按钮

时间:2017-01-06 22:55:09

标签: java javafx togglebutton

人,

很快:我想用CheckMenuItems(已完成)创建MenuButton。因为我想摆脱复选框,我希望CheckMenuItem在选中后切换(改变颜色),而是尝试使用ToggleButtons,但我无法将ToggleButtons / ToggleGroup放在MenuButton中。

感谢任何想法。

1 个答案:

答案 0 :(得分:1)

使用RadioMenuItem

即。只是做

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());
});