fxml combobox, get the selected value into javafx

Try this:

String output = Sample.getSelectionModel().getSelectedItem();
System.out.println(output);

I think the code you have in your question should work as long as the case of the combobox identifier in the code matches that of your fxml fx:id.

I modified this JavaFX fxml combo box selection demonstration app to add a button with an onAction method to retrieve a value from the combo box using the comboBox getValue() method and it worked fine for me.

Check the case of things, I notice that you say the fx:id is sample, yet in your code you use Sample - and the cases must match otherwise the fxml loader won't inject the node into your controller correctly.

Hard to say if the NullPointerException in your code is related to your combo box value retrieval issue as you don't say what the code at TW_JAVAFX_Undecorator.ButtonController.pruefen(ButtonController.java:60) is or provide full executable code to replicate the issue.


To get the ComboBox selected value, you can use Sample.getSelectionModel method.

Example:

myComboBox.getSelectionModel().selectedItemProperty()
    .addListener(new ChangeListener<String>() {
        public void changed(ObservableValue<? extends String> observable,
                            String oldValue, String newValue) {
            System.out.println("Value is: "+newValue);
        }
});