Limi Kastrati
Limi Kastrati

Reputation: 43

How to getText from ComobBox in javaFX?

If im going to get a text from text field than it would be like:

String username = txt_username.getText();

but how to get a text from ComboBox?

I tried this:

int TableNo = (int)comboBoxOrder.getItems();

Upvotes: 3

Views: 1377

Answers (1)

Matt
Matt

Reputation: 3187

comboBox.getValue() will return the selected object so you may want to .toString() it

public class MainNoFXML extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        ComboBox comboBox = new ComboBox(FXCollections.observableArrayList(new String[]{"Monday", "Tuesday", "Wednesday", "Thrusday", "Friday"}));
        comboBox.setOnAction(event -> {
            System.out.println("Selected:"+comboBox.getValue().toString());
            System.out.println("All:"+comboBox.getItems().toString());
        });

        Scene scene = new Scene(comboBox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Upvotes: 1

Related Questions