Using queries containing pseudo-classes in JavaFX
I am trying to use pseudo-classes in a programmatic query with help Node.lookupAll()
, however this seems to give unexpected results.
I've searched the web and can't find anything to suggest Node.lookupAll () doesn'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
+3
source to share
1 answer
This seems like a bug, or at least an undocumented feature. Nobody said otherwise and the JDK team took my bug report.
See JDK-8185831
You can use instead getPseudoClassStates().contains(...)
.
+1
source to share