JavaFX: Error message with ChangeListener

2.5k views Asked by At

While I was coding, I added the following piece of code:

nameComboBox.valueProperty().addListener(new ChangeListener<NameVO>() {

        @Override
        public void changed(
                ObservableValue<? extends NameVO> observable,
                NameVO oldValue, NameVO newValue) {
            // TODO Auto-generated method stub

        }
    });


And the Eclipse is showing me Red line under addListener and moving mouse over that I could see the following message:

The method addListener(ChangeListener<? super capture#6-of ?>) in the type ObservableValue<capture#6-of ?> is not applicable for the arguments (new ChangeListener<NameVO>(){})

Why would this be because of?

[Note: I have done similar thing for remaining ComboBoxes, but I am getting no such message over there]

3

There are 3 answers

0
Vikram On BEST ANSWER

Got the solution!

For all other ComboBoxes, the declaration involved the respective value object.

For example:

@FXML //  fx:id="projectComboBox"
private ComboBox<ProjectVO> projectComboBox; // Value injected by FXMLLoader

But, nameComboBox was simple declared as:

@FXML //  fx:id="nameComboBox"
private ComboBox<?> nameComboBox; // Value injected by FXMLLoader

Added NameVO at the place of ? and got rid of the problem.

1
Josh M On

It appears to be expecting ObservableValue<NameVO> as opposed to ObservableValue<? extends NameVO>.

To start off, look at how you are explicitly stating the type argument for your ChangeListener:

new ChangeListener<NameVO>{ .... }

You are explicitly stating that it is NameVO but when you actually go to implement the method, you are changing it to ? extends NameVO

1
Tohir On

The answer is simple, just add the below to your import

import javafx.scene.control.Toggle;