java 如何以编程方式在 JavaFX ComboBox 中设置字符串值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36758833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:49:48  来源:igfitidea点击:

How to programmatically set a string value in a JavaFX ComboBox

javajavafxcombobox

提问by Rahul Shah

Basically, here is what I need:

基本上,这是我需要的:

I have a JavaFX ComboBox, and it is set to Editable. Since it is editable, there is a little text field in there where someone can enter in a String. I want to use previously generated data to populate that little text field. How do I accomplish this?

我有一个 JavaFX ComboBox,它被设置为可编辑。因为它是可编辑的,所以那里有一个小的文本字段,人们可以在其中输入字符串。我想使用以前生成的数据来填充那个小文本字段。我该如何实现?

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

This is all i have in the way of relevant code and an "attempt" at a solution.

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

回答by jns

You can set the data items of a ComboBoxin the constructor:

可以ComboBox在构造函数中设置a的数据项:

 ObservableList<String> data = FXCollections.observableArrayList("text1", "text2", "text3");
 ComboBox<String> comboBox = new ComboBox<>(data);

or later:

或以后:

comboBox.setItems(data);

To select a data item, you can select the appropriate index in the SelectionModelor the item itself:

要选择数据项,您可以在SelectionModel或 项本身中选择适当的索引:

comboBox.getSelectionModel().select(0);
comboBox.getSelectionModel().select("text1");

It's also possible to set a value to the combobox editor, which is not contained in the underlying datamodel:

还可以为组合框编辑器设置一个值,该值不包含在底层数据模型中:

comboBox.setValue("textXXX");

回答by Jonatan Stenbacka

The "little text field" in a editable ComboBoxis known as the editorof the ComboBox. And it's a normal TextFieldobject. To access that object, you need to use the method ComboBox#getEditor(). This way you can use the methods of the TextFieldclass. If I understand you correctly, all you want to do is set the text of that TextField.

在一个可编辑的“小文本字段”ComboBox是被称为editorComboBox。而且它是一个普通的TextField物体。要访问该对象,您需要使用方法ComboBox#getEditor()。这样你就可以使用TextField类的方法。如果我理解正确的话,您要做的就是设置那个TextField.

This is done by doing comboBox.getEditor().setText(text)or comboBox.setValue(text). Both of these methods will set the text of the ComboBox.

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

But there's a difference when you want to fetch that text. ComboBox#getValue()ComboBox#getEditor()#getText()doesn't necessarily return the same value.

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

Consider the following example:

考虑以下示例:

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();
    }
}

If you change the text in the ComboBoxby chosing an alternative in the list, both ComboBox#valueProperty()and ComboBox#getEditor#textProperty()changes. But as you can see if you type something into the ComboBox, only the textProperty changes.

如果您ComboBox通过选择列表中的替代项来更改 中的文本,则ComboBox#valueProperty()和 都会ComboBox#getEditor#textProperty()更改。但是正如您所看到的,如果您在 中键入内容ComboBox,只有 textProperty 会发生变化。

So use whichever method you want when you set the text of the ComboBox, but be aware of the difference when you want to retrieve that text.

因此,在设置 的文本时使用您想要的任何方法ComboBox,但要注意检索该文本时的区别。