How to getText from ComobBox in javaFX?

Viewed 492

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();
1 Answers

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