在特定元素中的子代数更改后,如何运行方法?
它的功能应类似于下面的示例,该示例在特定元素的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");
}
}
答案 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);