[英]RadioButton // setUserData and getUserData don't work as expected
I am working with RadioButton
s and try to use the setUserData
method in order to identify them later on. 我正在使用RadioButton
并尝试使用setUserData
方法以便以后识别它们。 After educating myself on this topic, I've been thinking that I can setUserData
using the RadioButton
reference and getUserData
using the Toggle
reference later on, just as done in this Oracle article (Example 4-4). 在对这个主题进行了自我介绍之后,我一直在想, setUserData
可以使用RadioButton
引用来设置setUserData
,并使用Toggle
引用来获取getUserData
,就像在Oracle文章中一样 (示例4-4)。
Now, this hasn't been working out the way I wanted and led to a lot of frustration. 现在,这并没有解决我想要的方式,并导致很多挫败感。 I discovered, that I cannot get the same userData
I have set using the RadioButton
reference when applying getUserData
on the Toggle
reference that is returned after using group.getSelectedToggle()
. 我发现,在对使用group.getSelectedToggle()
之后返回的Toggle
引用应用getUserData
时,无法获得使用RadioButton
引用设置的相同userData
。 Does anybody know why? 有人知道为什么吗? Or how I can avoid this issue elegantly? 还是我可以优雅地避免这个问题?
I tried to make my issue clear in my SampleController.java file, which you can find below. 我试图在我的SampleController.java文件中弄清楚我的问题,您可以在下面找到它。 The for loop
you'll see is the first idea I came up with, but is definitely not a proper solution... 您将看到的for loop
是我想到的第一个主意,但绝对不是正确的解决方案...
My Main.java: 我的Main.java:
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/sample.fxml"));
primaryStage.setTitle("Demo");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
} catch (Exception e) {
System.out.println(e.toString() +
"\n" + e.getCause());
}
}
public static void main(String[] args) {
launch(args);
}
}
My sample.fxml: 我的sample.fxml:
<VBox id="VBox" prefHeight="200" prefWidth="320"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.SampleController">
<Label text="Select a time, then press the button" />
<fx:define>
<ToggleGroup fx:id="group" />
</fx:define>
<RadioButton id="rBtn1" text="Button 1" toggleGroup="$group" />
<RadioButton id="rBtn2" text="Button 2" toggleGroup="$group" />
<RadioButton id="rBtn3" text="Button 3" toggleGroup="$group" />
</VBox>
My SampleController.java: 我的SampleController.java:
public class SampleController implements Initializable {
@FXML
ToggleGroup group = new ToggleGroup();
RadioButton rBtn1 = new RadioButton();
RadioButton rBtn2 = new RadioButton();
RadioButton rBtn3 = new RadioButton();
@Override
public void initialize (URL url, ResourceBundle rb) {
rBtn1.setUserData("Button 1");
rBtn2.setUserData("Button 2");
rBtn3.setUserData("Button 3");
System.out.println(rBtn1.getUserData()); // prints 'Button 1'
System.out.println(group.getToggles().get(0).getUserData()); // prints 'null'
List<Toggle> toggles = group.getToggles();
int i = 1;
for (Toggle t : toggles) {
t.setUserData("Button " + i);
i++;
}
System.out.println(group.getToggles().get(0).getUserData()); // prints 'Button 1'
}
}
You forgot to use "fx:id" instead of "id" for the rBtn1... Which means that rBtn1 is not the same as the rBtn1 in your fxml file. 您忘记为rBtn1使用“ fx:id”而不是“ id” ...这意味着rBtn1与fxml文件中的rBtn1不同。
Changing: 更改:
<RadioButton id="rBtn1" text="Button 1" toggleGroup="$group" />
<RadioButton id="rBtn2" text="Button 2" toggleGroup="$group" />
<RadioButton id="rBtn3" text="Button 3" toggleGroup="$group" />
to: 至:
<RadioButton fx:id="rBtn1" text="Button 1" toggleGroup="$group" />
<RadioButton fx:id="rBtn2" text="Button 2" toggleGroup="$group" />
<RadioButton fx:id="rBtn3" text="Button 3" toggleGroup="$group" />
Will fix your problem. 将解决您的问题。
When doing this: 执行此操作时:
System.out.println(group.getToggles().get(0).getUserData()); // prints 'null'
You are referencing the rBtn1 from the fxml file, which you can reach by Node traversal from your group, which has a correct fx:id. 您正在从fxml文件引用rBtn1,可以通过组中的Node遍历来访问rBtn1,该组具有正确的fx:id。
When you are doing: 在执行操作时:
t.setUserData("Button " + i);
and redoing: 并重做:
System.out.println(group.getToggles().get(0).getUserData()); // prints 'null'
The value has been set through the group traversal and therefore returns the expected value. 该值已通过组遍历设置,因此返回期望值。
rBtn1.setUserData("Button 1");
is just setting the local class variable rBtn1's UserData. 只是设置局部类变量rBtn1的UserData。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.