如何以编程方式在JavaFX组合框中设置字符串值

如何以编程方式在JavaFX组合框中设置字符串值,java,javafx,combobox,Java,Javafx,Combobox,基本上,我需要的是: 我有一个JavaFX组合框,它被设置为可编辑。因为它是可编辑的,所以其中有一个小文本字段,用户可以在其中输入字符串。我想使用以前生成的数据来填充这个小文本字段。我如何做到这一点 enterSchoolName.setSelectionModel((SingleSelectionModel<String>) FXCollections.observableArrayList(studentData.getSchoolName())); enterScho

基本上,我需要的是:

我有一个JavaFX组合框,它被设置为可编辑。因为它是可编辑的,所以其中有一个小文本字段,用户可以在其中输入字符串。我想使用以前生成的数据来填充这个小文本字段。我如何做到这一点

    enterSchoolName.setSelectionModel((SingleSelectionModel<String>) FXCollections.observableArrayList(studentData.getSchoolName()));
enterSchoolName.setSelectionModel((SingleSelectionModel)FXCollections.observableArrayList(studentData.getSchoolName());

这是我在相关代码和解决方案“尝试”方面的全部内容。

您可以在构造函数中设置组合框的数据项:

 ObservableList<String> data = FXCollections.observableArrayList("text1", "text2", "text3");
 ComboBox<String> comboBox = new ComboBox<>(data);
要选择数据项,您可以在
SelectionModel
中选择适当的索引或项本身:

comboBox.getSelectionModel().select(0);
comboBox.getSelectionModel().select("text1");
还可以为组合框编辑器设置一个值,该值不包含在基础数据模型中:

comboBox.setValue("textXXX");
可编辑的组合框中的“小文本字段”称为组合框的
编辑器。它是一个普通的
TextField
对象。要访问该对象,需要使用方法
组合框#getEditor()
。这样,您可以使用
TextField
类的方法。如果我理解正确,您所要做的就是设置该
文本字段的文本

这是通过执行
comboBox.getEditor().setText(text)
comboBox.setValue(text)
来完成的。这两种方法都将设置组合框的文本

但是,当您想要获取该文本时,这是有区别的
ComboBox#getValue()
ComboBox#getEditor()#getText()
不一定返回相同的值

考虑以下示例:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TestComboBox extends Application {
    @Override
    public void start(Stage stage) {
        ComboBox<String> comboBox = new ComboBox<String>();
        comboBox.setEditable(true);
        comboBox.setValue("Test");
        comboBox.getItems().addAll("Test", "Test2", "Test3");

        VBox content = new VBox(5);
        content.getChildren().add(comboBox);
        content.setPadding(new Insets(10));

        GridPane valueGrid = new GridPane();

        Label cbValue = new Label();
        cbValue.textProperty().bind(comboBox.valueProperty());
        Label cbText = new Label();
        cbText.textProperty().bind(comboBox.getEditor().textProperty());

        valueGrid.add(new Label("ComboBox value: "), 0, 0);
        valueGrid.add(new Label("ComboBox text: "), 0, 1);
        valueGrid.add(cbValue, 1, 0);
        valueGrid.add(cbText, 1, 1);

        content.getChildren().add(valueGrid);

        stage.setScene(new Scene(content));
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}
导入javafx.application.application;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.Label;
导入javafx.scene.layout.GridPane;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类TestComboBox扩展了应用程序{
@凌驾
公众假期开始(阶段){
ComboBox ComboBox=新建ComboBox();
comboBox.setEditable(真);
comboBox.setValue(“测试”);
comboBox.getItems().addAll(“Test”、“Test2”、“Test3”);
VBox内容=新的VBox(5);
content.getChildren().add(组合框);
内容。设置填充(新插图(10));
GridPane valueGrid=新建GridPane();
标签cbValue=新标签();
cbValue.textProperty().bind(comboBox.valueProperty());
Label cbText=新标签();
cbText.textProperty().bind(comboBox.getEditor().textProperty());
添加(新标签(“组合框值:”),0,0);
添加(新标签(“组合框文本”)、0、1;
valueGrid.add(cbValue,1,0);
valueGrid.add(cbText,1,1);
content.getChildren().add(valueGrid);
舞台场景(新场景(内容));
stage.show();
}
公共静态void main(字符串[]args){
发射();
}
}
如果通过选择列表中的替代项来更改组合框中的文本,则组合框#valueProperty()
和组合框#getEditor#textProperty()
都会更改。但是,正如您所看到的,如果您在
组合框
中键入了一些内容,则只有textProperty发生了更改

因此,当您设置
组合框
的文本时,请使用您想要的任何方法,但当您想要检索该文本时,请注意差异

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TestComboBox extends Application {
    @Override
    public void start(Stage stage) {
        ComboBox<String> comboBox = new ComboBox<String>();
        comboBox.setEditable(true);
        comboBox.setValue("Test");
        comboBox.getItems().addAll("Test", "Test2", "Test3");

        VBox content = new VBox(5);
        content.getChildren().add(comboBox);
        content.setPadding(new Insets(10));

        GridPane valueGrid = new GridPane();

        Label cbValue = new Label();
        cbValue.textProperty().bind(comboBox.valueProperty());
        Label cbText = new Label();
        cbText.textProperty().bind(comboBox.getEditor().textProperty());

        valueGrid.add(new Label("ComboBox value: "), 0, 0);
        valueGrid.add(new Label("ComboBox text: "), 0, 1);
        valueGrid.add(cbValue, 1, 0);
        valueGrid.add(cbText, 1, 1);

        content.getChildren().add(valueGrid);

        stage.setScene(new Scene(content));
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}