gpt4 book ai didi

java - 检索 ToggleGroup 名称 JavaFX

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:46 26 4
gpt4 key购买 nike

我正在尝试检索给定 ToggleButton 的 ToggleGroup 的“名称”(或任何类型的唯一标识符),但没有成功。如果可能的话,不同的按钮共享事件处理程序是很重要的。有什么帮助吗?

//create toggleGroups
final ToggleGroup toggleGroup1 = new ToggleGroup();
final ToggleGroup toggleGroup2 = new ToggleGroup();

//create toggleButtons
final ToggleButton toggleButton1 = new ToggleButton("b1");
final ToggleButton toggleButton2 = new ToggleButton("b2");

//assign toggleButtons to toggleGroups
toggleButton1.setToggleGroup(toggleGroup1);
toggleButton2.setToggleGroup(toggleGroup2);

//assign custom handler to toggleButtons
toggleButton1.setOnAction(new handleEvent());
toggleButton2.setOnAction(new handleEvent());

//handler
class handleEvent implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {

// enable ability to get toggleButton name later
ToggleButton b = (ToggleButton)e.getSource();

//get name of group toggleButton belongs too?
String toggleGroup = b.getToggleGroup().toString();

// do stuff based on name and group
if (toggleGroup == "toggleGroup1") {
//do stuff
} else if (toggleGroup == "toggleGroup2") {
//d other stuff
}
}
}

最佳答案

您可以将名称设置为 userData:toggleGroup.setUserData("toggleGroupName")

要检索它,请调用:String name = (String)toggleGroup.getUserData()

关于java - 检索 ToggleGroup 名称 JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com