问题
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