I need to test the value of a selected item to call different methods, so I write this code adding a listener, but the code generate a syntax error
@FXML
private JFXComboBox<String> cmbComp;
cmbComp.valueProperty().addListener(new ChangeListener<String>() {
public void changed(ObservableValue<String> composant, String oldValue, String newValue) throws SQLException {
if(/*test item value*/){
/*do something*/
}else{
/*do other thing*/
}
}
});
also I don't need an old value and a new one, just test the selected value, how can'I pass arguments ?
We can also create a combo box by using an empty constructor and call its setItems method to set the data. ComboBox comboBox = new ComboBox(options); comboBox. setItems(options); To add more items to the combobox of items with new values.
You can create a combo box by instantiating the javafx. scene. control. ComboBox class.
One solution that is a bit more straightforward and avoids some extra lines of code is adding an action listener (ideally from the scene builder) to the combobox, like this:
private void comboAction(ActionEvent event) {
System.out.println(comboBox_DbTables.getValue());
}
In case anybody missed it, OP answers in the post:
I found the error, here is the new code, I hope it helps others
cmbComp.getSelectionModel().selectedItemProperty().addListener((options, oldValue, newValue) -> {
System.out.println(newValue);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With