JavaFX: why can't be compile no matter what kind of `changeListener` I passed to `textArea.getKeyPressedProperty().addListener`

StackOverflow https://stackoverflow.com/questions/23578765

Question

JavaFx code:

public class MyController implements Initializable {
    @FXML
    private TextArea editor;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        editor.onKeyReleasedProperty().addListener(new ChangeListener<KeyEvent>() {
            @Override
            public void changed(ObservableValue<? extends KeyEvent> o, KeyEvent oldVal, KeyEvent newVal) {
                System.out.println("changed!");
            }
        });
    }
}

This code can't be compiled on the lines of new ChangeListener..., and the error is:

Can't resolve method 'addListener
  (javafx.beans.value.ChangeListener<javafx.scene.input.KeyEvent>>)'

I have changed the code again and again, but I can't find a way to make it compile.

(I'm using Intellij-IDEA 13 + jdk 1.7)

Was it helpful?

Solution 2

editor.onKeyReleasedProperty().addListener(new ChangeListener<EventHandler<? super KeyEvent>> () {
  @Override
  public void changed(ObservableValue<? extends EventHandler<? super KeyEvent>> observable, EventHandler<? super KeyEvent> oldValue, EventHandler<? super KeyEvent> newValue) {
    System.out.println("changed!");
  }
});

OTHER TIPS

Are you sure you want add a listener to onKeyReleasedProperty() property?

I guess you are seeking for

editor.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent event) {
    }
});

Or in short as

editor.setOnKeyReleased(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent event) {
    }
});

Otherwise assylias's answer is correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top