gpt4 book ai didi

java - togglegroup.getSelectedToogle() 始终返回 null JavaFX

转载 作者:行者123 更新时间:2023-11-30 06:02:52 25 4
gpt4 key购买 nike

我有这段代码可以从选定的ToggleGroup中获取值。

但是,当我运行此代码时,我得到一个空列表。我似乎不知道如何解决这个问题。

   public TestPane(TestService service) {
this.service = service;

this.setPrefHeight(300);
this.setPrefWidth(750);

this.setPadding(new Insets(5, 5, 5, 5));
this.setVgap(5);
this.setHgap(5);

questionField = new Label();
vragen = service.getAllQuestions();
Question question = vragen.iterator().next();
questionField.setText(question.getQuestion());
vragen.remove(question);

add(questionField, 0, 0, 1, 1);

statementGroup = new ToggleGroup();
Set<String> statements = question.getStatements();
System.out.println(statements);
int i = 1;
for (String st : statements) {
radio = new RadioButton(st);
radio.setText(st);
radio.setUserData(st);
radio.setToggleGroup((ToggleGroup) statementGroup.getUserData());
add(radio, 0, i++);
}

submitButton = new Button("Submit");
add(submitButton, 0, i);
submitButton.setOnAction(new SubmitHandler(this,service));
}

public List<String> getSelectedStatements() {
List<String> selected = new ArrayList<String>();
if (statementGroup.getSelectedToggle() != null) {
selected.add(statementGroup.getSelectedToggle().getUserData().toString());
}
return selected;
}

最佳答案

以下行不正确:

radio.setToggleGroup((ToggleGroup) statementGroup.getUserData());

正确的调用是:

radio.setToggleGroup(statementGroup);

回答“为什么”的问题:ToggleGroupgetSelectedToggle 方法始终返回 null,因为没有与之关联的切换,如

(ToggleGroup) statementGroup.getUserData()

返回null,因此实际上会执行radio.setToggleGroup(null),这意味着:RadioButton没有切换组。

关于java - togglegroup.getSelectedToogle() 始终返回 null JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51910781/

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