JavaFX:初始化 RadioButton 选择状态
Posted
技术标签:
【中文标题】JavaFX:初始化 RadioButton 选择状态【英文标题】:JavaFX: Initialize RadioButton selection status 【发布时间】:2018-03-09 21:26:12 【问题描述】:我需要保存多个 RadioButtons 的选择状态,以便稍后回到场景时可以看到选择了哪个 RadioButton。和userData无关,就是要看看有没有被选中。现在我知道如何让它工作,但是有很多乱七八糟的复制和粘贴。每个 ToggleGroup 都是这样的:
@FXML private RadioButton rb1;
@FXML private RadioButton rb2;
public static int[] status = new int[600];
// to save it
if (rb1.getSelect(true))
status[0] = 1;
else
status[0] = 0;
// to load it
if (status[0] == 1)
rb1.setSelected(true);
else
rb2.setSelected(true);
问题是我编写了一个包含 300 多个问题的调查,并带有二进制答案。所以我有 600 多个不同的 RadioButtons。以这种方式实施它需要几个小时。
有什么聪明的方法吗?我很感激任何建议。提前致谢!
【问题讨论】:
通常GUI不应该存储任何状态,但它应该代表一个模型,模型的状态应该决定GUI元素的状态。比如:你有一个包含Question
实例的Survey
类。每个Question
实例都有一个BooleanProperty
(答案),并在GUI 上表示为RadioButton
。每个单选按钮的选中属性现在可以双向绑定到问题 answerpropery 之一。然后你只存储Survey
(在内存中,持久化 - 如你所愿)。
@DVarga 但我必须创建 300 个 Question 实例,每个实例都有 getter 和 setter?您可以举一个关于如何将 BooleanProperty 绑定到 RadioButton 的示例吗?
【参考方案1】:
这是一个 SCVExample,其中包含基于我的评论的最简单的实现:它定义了一个模型(Survey
和 Question
)然后将 GUI 绑定到这个模型。
public class Radios extends Application
class Survey
private ObservableList<Question> questions = FXCollections.observableArrayList();
public ObservableList<Question> getQuestions()
return questions;
class Question
private StringProperty text = new SimpleStringProperty("");
private BooleanProperty answer = new SimpleBooleanProperty(false);
public Question(String text)
setText(text);
public boolean isAnswer()
return answer.get();
public BooleanProperty answerProperty()
return answer;
public void setAnswer(boolean answer)
this.answer.set(answer);
public String getText()
return text.get();
public StringProperty textProperty()
return text;
public void setText(String text)
this.text.set(text);
@Override
public void start(Stage primaryStage) throws Exception
// Model
Survey survey = new Survey();
for (int i = 0; i<300; i++)
Question question = new Question("Do you like number " + i + "?");
question.answerProperty().addListener((obs, oldval,newval) ->
System.out.println("Question: " + question.getText() + " answer changed from " + oldval + " to " + newval);
);
survey.getQuestions().add(question);
// View
VBox root = new VBox();
root.setSpacing(10);
for (Question question : survey.getQuestions())
VBox vBox = new VBox();
vBox.setSpacing(5);
HBox answerHBox = new HBox();
answerHBox.setSpacing(20);
vBox.getChildren().addAll(new Label(question.getText()), answerHBox);
RadioButton yes = new RadioButton("Yes");
RadioButton no = new RadioButton("No");
ToggleGroup toggleGroup = new ToggleGroup();
yes.setToggleGroup(toggleGroup);
no.setToggleGroup(toggleGroup);
answerHBox.getChildren().addAll(yes, no);
yes.setSelected(question.isAnswer());
no.setSelected(!question.isAnswer());
toggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) ->
question.setAnswer(newValue.equals(yes));
);
root.getChildren().add(vBox);
Scene scene = new Scene(new ScrollPane(root), 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
public static void main(String[] args)
launch(args);
这将生成如下调查:
控制台输出:
Question: Do you like number 1? answer changed from false to true
Question: Do you like number 3? answer changed from false to true
Question: Do you like number 6? answer changed from false to true
Question: Do you like number 8? answer changed from false to true
Question: Do you like number 8? answer changed from true to false
Question: Do you like number 8? answer changed from false to true
【讨论】:
可以将两个 RadioButtons 绑定到一个问题(因为我对每个问题都有两个可能的答案)或者是否可以选择将 ToggleGroup 本身绑定到问题? 为你更新了答案。以上是关于JavaFX:初始化 RadioButton 选择状态的主要内容,如果未能解决你的问题,请参考以下文章
python tkinter 组件功能实例总结(代码+效果图)(Radiobutton | Button | Entry | Menu | Text)
android 中如何获取radiogroup 中那个radiobutton被选择