【问题标题】:How to set combo box value to array value when combo box is not uniquely identified?当组合框不是唯一标识时,如何将组合框值设置为数组值?
【发布时间】: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 个就足够了 :)

标签: java javafx combobox fxml


【解决方案1】:

我猜您可能只使用ListList 索引就可以解决您的问题。不能 100% 确定,因为您的问题缺少 MCVE

如果你需要设置UserData,我认为应该在创建ComboBox的时候完成。

例子:

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication329 extends Application
{

    ComboBox<String> mainComboBox = new ComboBox();
    List<ComboBox<String>> comboBoxes = new ArrayList();
    String[] data = "ABCDE".split("");
    String[] value = "01234".split("");

    @Override
    public void start(Stage primaryStage)
    {
        mainComboBox.setItems(FXCollections.observableArrayList(value));
        mainComboBox.setValue("0");

        for (int i = 0; i < data.length; i++) {
            ComboBox<String> tempComboBox = new ComboBox<>(FXCollections.observableArrayList(data));
            tempComboBox.setUserData("ComboBox" + i);//If you need to set the UserData or you can just use the List index
            tempComboBox.setValue(data[i]);
            comboBoxes.add(tempComboBox);
        }

        mainComboBox.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {
            for (int i = 0; i < data.length; i++) {
                String tempValue = data[(newValue.intValue() + i) % 5];
                comboBoxes.get(i).setValue(tempValue);
            }
        });

        VBox vBox = new VBox(mainComboBox);
        vBox.getChildren().addAll(comboBoxes);

        StackPane root = new StackPane(vBox);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

【讨论】:

  • 非常感谢!在当前的形式下,无法在设置值时设置它们,但是您已经让我了解了如何使用它,以便在我想加载数据并使用时重新设置它关键是每次都这样做。明天我将对此进行更详细的调查。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多