Scene.cursorProperty()如何绑定或添加侦听器

时间:2013-02-21 00:38:51

标签: javafx scene

我想知道如何从Scene对象添加侦听器或绑定cursorProperty()方法。我试图通过当前光标类型调整场景的大小来检测。

1 个答案:

答案 0 :(得分:0)

不是最干净的方法,但它帮助我找出发生了什么:

@Override
public void start(final Stage stage) {

    // .. initialize scene

    addCursorChangeListeners(mainPane);
    scene.cursorProperty().addListener(new ChangeListener<Cursor>() {
        @Override
        public void changed(final ObservableValue<? extends Cursor> observable, final Cursor oldValue, final Cursor newValue) {
            LOGGER.info("Cursor changed for " + scene + " from: " + oldValue + " to: " + newValue);
        }
    });
}

private void addCursorChangeListeners(final Parent parent) {
    for (final Node child : parent.getChildrenUnmodifiable()) {
        child.cursorProperty().addListener(new ChangeListener<Cursor>() {
            @Override
            public void changed(final ObservableValue<? extends Cursor> observable, final Cursor oldValue, final Cursor newValue) {
                LOGGER.info("Cursor changed for " + child + " from: " + oldValue + " to: " + newValue);
            }
        });
        if (child instanceof Parent) {
            addCursorChangeListeners((Parent) child);
        }
    }
}

Node稍后添加的内容不在此处。