当组合框未唯一标识时,如何将组合框值设置为数组值?

时间:2019-01-30 21:09:19

标签: java javafx combobox fxml

我有一个密钥,我想将其设置为使用循环生成的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(在其中生成了replaceKeyEditorView的位置)执行所有这些操作。在这里,我有一个char数组字母[26],我想将由replacementKeyEditorView生成的每个组合框的值设置为此数组中特定索引的值。

我希望这是有道理的!

1 个答案:

答案 0 :(得分:0)

我猜测您仅使用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);
    }

}
相关问题