Using queries containing pseudo-classes in JavaFX

老子叫甜甜 提交于 2020-01-24 11:28:12

问题


I'm tring to use pseudo classes in programmtic query using Node.lookupAll() however this seems to give unexpected results.

I've searched online and can't find anything to suggest Node.lookupAll() wouldn't support psuedo classes...

public class Foo extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        PseudoClass pseudoClass = PseudoClass.getPseudoClass("custom");

        Label a = new Label();
        a.getStyleClass().add("foo");
        a.pseudoClassStateChanged(pseudoClass, false);

        Label b = new Label();
        b.getStyleClass().add("foo");
        b.pseudoClassStateChanged(pseudoClass, true);

        Label c = new Label();
        c.getStyleClass().add("foo");
        c.pseudoClassStateChanged(pseudoClass, true);


        HBox box = new HBox(a, b, c);
        primaryStage.setScene(new Scene(box));

        System.out.println(box.lookupAll(":custom").size()); // expected 2
        System.out.println(box.lookupAll(".foo:custom").size()); // expected 2
        System.out.println(box.lookupAll(".foo").size()); // expected 3, got 3
        System.out.println(box.lookupAll(":magichorse").size()); // expected 0 !!

    }

}

Output

4
3
3
4

回答1:


This does appear to be a bug, or at least undocumented feature. No one has said otherwise, and the JDK team have accepted my bug report.

See JDK-8185831

As a workaround getPseudoClassStates().contains(...) can be used instead.



来源:https://stackoverflow.com/questions/45207442/using-queries-containing-pseudo-classes-in-javafx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!