0

I implemented a ComboBox which is editable and autocompletes itself similar to the one in this answer: https://stackoverflow.com/a/27384068/9611276

Now i want to add a listener which performs some action when the value in this Combobox changes. Something like this:

combobox.getValueProperty().addListener((obs, oldV, newV) -> {...});

The problem is, that i cannot access the combobox.getValueProperty() without getting a java.lang.ClassCastException. In the answer above it is mentioned to get the combobox value with the following function:

    public static <T> TgetComboBoxValue(ComboBox<T> comboBox) {
    if (comboBox.getSelectionModel().getSelectedIndex() < 0) {
        return null;
    } else {
        return comboBox.getItems().get(comboBox.getSelectionModel().getSelectedIndex());
    }
}

How do i create and access the property to add a listener?

Thanks, BR

lospollos
  • 45
  • 5
  • 1
    To be honest, I tried the "answer" to that question and there were many serious bugs that I couldn't continue with it. I provided in my opinion a much better solution based on some of the other answers in there whilst fixing some of its bugs. https://stackoverflow.com/a/55709876/9278333 I have it in a production environment and it is working extremely well (even with 10,000+ possible values). There is also a path forward to Java 9+ once you upgrade. – trilogy Feb 26 '20 at 13:57

1 Answers1

-2

You can add a propertyChangeListener to that combobox

 comboBox.addPropertyChangeListener(evt -> {

 });

Or additionally, you can fire your own property change event. Where 2 is the old value and 3 is the new value of combobox. It can take strings, booleans and other primitive types too.

 comboBox.firePropertyChange("samplePropertyChange", 2, 3);
yed2393
  • 262
  • 1
  • 12