JavaFX-子计数更改事件

时间:2018-08-12 17:36:25

标签: java javafx

在特定元素中的子代数更改后,如何运行方法?

它的功能应类似于下面的示例,该示例在特定元素的text属性更改后运行一个方法。

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;

public class Controller
{
    @FXML
    private TextArea textArea;

    @FXML
    private void initialize()
    {
        textArea.textProperty().addListener(e -> onTextPropertyChanged());
    }

    private void onTextPropertyChanged()
    {
        System.out.println("cabbage");
    }
}

1 个答案:

答案 0 :(得分:0)

ObservableList允许您添加InvalidationListener

final ObservableList<Node> children = pane.getChildren();

InvalidationListener listener = new InvalidationListener() {

    private int size = children.size();

    @Override
    public void invalidated(Observable o) {
        int newSize = children.size();
        if (size != newSize) { // prevent triggering if the size did not change
            size = newSize;
            // TODO: add some logic
        }
    }

};
children.addListener(listener);