PseudoClass:来自在设置自定义状态时中断状态的通知

时间:2018-12-02 13:36:36

标签: javafx pseudo-class

下面是一个示例,它的行为不像我期望的那样-希望找出行为或我的期望是否错误(很可能是我做错了;)。

它的作用:

  • 它使用自定义伪类实现自定义按钮,其方式与code example in the java doc一样
  • 它在按钮的getPseudoClassStates()返回的observableSet上注册了一个侦听器,该监听器记录了更改时的活动状态
  • 它具有ui(一个简单的按钮),可切换自定义按钮的特殊状态

预期行为:通过鼠标/键盘(悬停,聚焦,准备好……)与自定义按钮交互的通知

实际行为:如果未设置特殊状态,则与预期相同,一旦设置了特殊状态,便不会发出通知

问题:

  • 错误或功能?
  • 如何更改设置以始终得到通知?

示例:

public class PseudoStateNotification extends Application {
    private SpecialButton specialButton;
    private SetChangeListener sl;

    public static class SpecialButton extends Button {
        private static PseudoClass SPECIAL = PseudoClass.getPseudoClass("special");
        private BooleanProperty special = new SimpleBooleanProperty(this, "special", false) {

            @Override
            protected void invalidated() {
                pseudoClassStateChanged(SPECIAL, get());
            }

        };

        public SpecialButton(String text) {
            super(text);
        }

        public void setSpecial(boolean sp) {
            special.set(sp);
        }

        public boolean isSpecial() {
            return special.get();
        }

        public BooleanProperty specialProperty() {
            return special;
        }
    }

    private Parent createContent() {
        sl = change -> LOG.info("pseudo-changed: " + change.getSet());
        specialButton =  new SpecialButton("custom buttom");
        specialButton.getPseudoClassStates().addListener(sl);
        BorderPane pane = new BorderPane(specialButton);
        Button toggle = new Button("toggle special state");
        toggle.setOnAction(e -> {
            specialButton.setSpecial(!specialButton.isSpecial());
        });
        pane.setBottom(toggle);
        return pane;
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(new Scene(createContent(), 200, 200));
        URL uri = getClass().getResource("pseudo.css");
        stage.getScene().getStylesheets().add(uri.toExternalForm());
        stage.setTitle(FXUtils.version());
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    @SuppressWarnings("unused")
    private static final Logger LOG = Logger
            .getLogger(PseudoStateNotification.class.getName());

}

样式表pseudo.css:

.button:special {
  -fx-text-fill: red;
}

更新:

Jose's answer中很好地跟踪过,其根本原因是ObservaleSet<PseudoClass> Node.getPseudoClassStates()返回一个围绕状态的自由飞行的,不可修改的包装,这使得返回的集合可以被垃圾回收。在我的书that's a bug中-例如TableView.getVisibleLeafColumns()的可比api已正确实现,并且对包装器有很强的引用。解决方法是在客户端代码中强烈引用该集合。

1 个答案:

答案 0 :(得分:4)

这与自定义状态无关,但通常是弱听者在某些时候被垃圾回收的情况:您的SetChangeListener将在一段时间后消失。

您可以完全不单击切换按钮来进行验证。只需将按钮悬停几次,然后单击它。过了一点,您将不会收到任何通知。

例如,这是一系列更改:

INFO: pseudo-changed: [focused]
INFO: pseudo-changed: [hover, focused]
INFO: pseudo-changed: [focused]
INFO: pseudo-changed: [hover, focused]
INFO: pseudo-changed: [focused]
INFO: pseudo-changed: [hover, focused]
INFO: pseudo-changed: [hover, pressed, focused]
INFO: pseudo-changed: [hover, pressed, focused, armed]
INFO: pseudo-changed: [hover, focused, armed]
INFO: pseudo-changed: [hover, focused]
INFO: pseudo-changed: [hover, pressed, focused]
INFO: pseudo-changed: [hover, pressed, focused, armed]
INFO: pseudo-changed: [hover, focused, armed]
INFO: pseudo-changed: [hover, focused]
INFO: pseudo-changed: [hover, pressed, focused]
INFO: pseudo-changed: [hover, pressed, focused, armed]
INFO: pseudo-changed: [hover, focused, armed]
INFO: pseudo-changed: [hover, focused]
INFO: pseudo-changed: [focused]
INFO: pseudo-changed: [hover, focused]

在最后一个之后,我不再收到通知。

要避免这种情况,我们需要做的就是对可观察到的伪类集进行严格引用:

private ObservableSet<PseudoClass> states;

private Parent createContent() {
    sl = change -> LOG.info("pseudo-changed: " + change.getSet());
    specialButton =  new SpecialButton("custom buttom");
    states = specialButton.getPseudoClassStates();
    states.addListener(sl);
    ...
}

现在,它可以通过按钮或切换按钮进行任何状态更改。

INFO: pseudo-changed: [hover, focused]
INFO: pseudo-changed: [focused]
INFO: pseudo-changed: []
INFO: pseudo-changed: [special]
INFO: pseudo-changed: [hover, special]
INFO: pseudo-changed: [hover, pressed, special]
INFO: pseudo-changed: [hover, pressed, focused, special]
...
INFO: pseudo-changed: [focused, special]
INFO: pseudo-changed: [special]
INFO: pseudo-changed: []
INFO: pseudo-changed: [hover]
...

编辑

虽然finalize已被弃用,但在第一种情况下,它仍可用于检查侦听器是否已被调试:

specialButton.getPseudoClassStates().addListener(new SetChangeListener<PseudoClass>() {
        @Override
        public void onChanged(Change<? extends PseudoClass> change) {
            LOG.info("pseudo-changed: " + change.getSet());
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
            LOG.info("SetChangeListener finalized by gc");
        }
    });

因此,在以上测试中,您将获得:

...
INFO: pseudo-changed: [hover, focused]
INFO: pseudo-changed: [focused]
INFO: pseudo-changed: [hover, focused]
INFO: SetChangeListener finalized by gc

,此后没有更多通知,如前所述。

但是,如果您拥有强引用,即使您明确调用System.gc(),也不会对监听器进行gc操作。

还值得检查FXCollection::UnmodifiableObservableSet的实现,其中WeakSetChangeListenerused,同时保留对侦听器本身的引用:

private SetChangeListener<E> listener;
private void initListener() {
        if (listener == null) {
            listener = c -> {
                callObservers(new SetAdapterChange<E>(UnmodifiableObservableSet.this, c));
            };
            this.backingSet.addListener(new WeakSetChangeListener<E>(listener));
        }
    }

这意味着引用private SetChangeListener<E> listener;确实不是必需的。