【发布时间】:2019-06-24 06:20:30
【问题描述】:
我有一个键,我想将它设置为我使用循环生成的 26 个组合框中的每一个的值,因此没有我可以调用来设置值的 fx:id。
我有一个 GUI,它要么包含 26 个组合框,要么包含一个微调器,具体取决于特定其他组合框的值。这些是由枚举器自动生成的,该枚举器将它们加载到我的 GUI 上的空白处(我已经设置了 26 个组合框,由我对其中的 1 个进行编码(它们内部都有相同的值)并使用循环生成其中 26 个)。 对于 26 个组合框,我有一个 char 数组 [26],我想将每个组合框设置为数组中的不同值,但由于它们是使用循环生成的,我似乎无法设置唯一的 fx :id 到每个组合框,以便我可以调用它们来设置值。 我想我需要以某种方式将它们绑定到数组,但我对 javafx/fxml 很陌生,所以我不知道该怎么做。
这会生成组合框:
private SubstitutionKey key;
public SubstitutionKeyEditorView(SubstitutionKey key) {
this.key = key;
FXMLLoader loader = new FXMLLoader(getClass().getResource("SubstitutionKeyEditor.fxml"));
loader.setController(this);
loader.setRoot(this);
try {
loader.load();
for (char x = 'A'; x <= 'Z'; x++) {
getChildren().add(new LetterSelectorView(String.valueOf(x), this)); //add 26 letter selectors (26 combo boxes)
}
} catch (IOException exc) {
throw new RuntimeException(exc);
}
}
SubstitutionKeyEditor.fxml 仅将类型设置为 flowPane,以便它们很好地显示。
这是 letterSelectorView:
@FXML
private Label letterLabel;
@FXML
private ComboBox letterSelection;
public LetterSelectorView(String letter, LetterChangedListener listener) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("LetterSelector.fxml"));
//add the text and combo box to the place being loaded into
loader.setController(this);
loader.setRoot(this);
try {
loader.load();
letterLabel.setText(letter); //give the label the correct letter
letterSelection.getItems().add(" "); //add blank
for (char x = 'A'; x <= 'Z'; x++) {
letterSelection.getItems().add(String.valueOf(x)); //add all the letters to each combo box
}
letterSelection.valueProperty().addListener((obs, oldValue, newValue) -> listener.letterChanged(letter, (String) newValue));
//if value of combo box changes, rerun cipher
} catch (IOException exc) {
throw new RuntimeException(exc);
}
}
我正在尝试从另一个控制器 CipherController (这是生成替换密钥编辑器视图的位置)执行所有这些操作。在这里,我有一个 char 数组 letters[26] ,我想将substitutionKeyEditorView 生成的每个组合框的值设置为该数组中特定索引的值。
我希望这是有道理的!
【问题讨论】:
-
使用 node.setUserData().
-
@Sedrick 很抱歉可能很愚蠢,但我应该在哪里使用它?在密码 GUI 的控制器中或当我设置组合框时?
-
请提供一个minimal reproducible example 来证明问题(并尽量减少:不需要 26 个连击,2 个或最多 3 个就足够了 :)