Damian Szarik Paul
Damian Szarik Paul

Reputation: 99

ComboBox select specific object id

I have a ComboBox<Category> which I can easily fill with my ObservableList<Category>.

I want in a initialization popup window to set that ComboBox value to a specific Category.getName(). How can I achieve that?

Upvotes: 2

Views: 485

Answers (1)

jewelsea
jewelsea

Reputation: 159291

Select the required item in the SelectionModel of the ComboBox.

comboBox.getSelectionModel().select("oranges");

Here is a sample app to demo this:

combodemo

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class ComboControl extends Application {
    @Override public void start(Stage stage) {
        ComboBox<String> comboBox = new ComboBox<String>();
        comboBox.getItems().addAll(
                "apples",
                "oranges",
                "pears"
        );

        comboBox.getSelectionModel().select("oranges");

        stage.setScene(new Scene(new Pane(comboBox)));
        stage.show();
    }

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

Upvotes: 2

Related Questions