Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically set a string value in a JavaFX ComboBox

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?

    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.

like image 556
Rahul Shah Avatar asked Apr 21 '16 03:04

Rahul Shah


2 Answers

You can set the data items of a ComboBox in the constructor:

 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 SelectionModel or the item itself:

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");
like image 158
jns Avatar answered Sep 28 '22 14:09

jns


The "little text field" in a editable ComboBox is known as the editor of the ComboBox. And it's a normal TextField object. To access that object, you need to use the method ComboBox#getEditor(). This way you can use the methods of the TextField class. If I understand you correctly, all you want to do is set the text of that 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.

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

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 ComboBox by 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.

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.

like image 34
Jonatan Stenbacka Avatar answered Sep 28 '22 16:09

Jonatan Stenbacka