javafxfxml

JavaFX FXML - Does Button have OnEdit Like Method?


I have a class that extends Button where a String is constructed like so,

text = button.getText();

However, I need to always update text when it changes i.e. a listener. Does Button have a method like onEdit where I can make sure the button's text is always the same as the variable text?


Solution

  • Register a listener with the button's text property:

    button.textProperty().addListener((obs, oldText, newText) -> {
        // do whatever you need with newText
    });