JavaFX – use FX: ID as CSS ID in fxml

It seems that if no ID (CSS) is specified in F XML, the FX: ID value is used by default My previous understanding is that the two are completely disjoint. The ID is CSS and only CSS FX: ID of @ fxml binding in the controller

This can be proved by a small test - three buttons, the first is ID, the second is FX: ID, and the third is two types of ID

<V@R_738_2419@ maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button id="cssid0" mnemonicParsing="false" text="Button" />
      <Button fx:id="fxid1" mnemonicParsing="false" text="Button" />
      <Button id="cssid2" fx:id="fxid2" mnemonicParsing="false" text="Button" />
   </children>
</V@R_738_2419@>

Use node Lookup (cssselector) allows selection by FX: ID

@Override
public void start(Stage stage) throws Exception {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/foo.fxml"));
    Parent p = loader.load();

    System.out.println(p.lookup("#cssid0")); // => Button[id=cssid0,styleClass=button]'Button'
    System.out.println(p.lookup("#fxid1"));  // => Button[id=fxid1,styleClass=button]'Button'

    System.out.println(p.lookup("#fxid2"));  // => null (if there is a CSS ID it takes precedent)
    System.out.println(p.lookup("#cssid2")); // Button[id=cssid2,styleClass=button]'Button'

    stage.setScene(new Scene(p,200,200));
    stage.getScene().getStylesheets().add("/foo.css");
    stage.show();
}

CSS also allows selection through FX: ID

#cssid0 {
    -fx-color: red;
}
#fxid1 {
    -fx-color: green;
}
#cssid2 {
    -fx-color: blue;
}

What's the difference between FX: ID and ID: in JavaFX?, Javadoc for Node. Getid () or anywhere else I can find doesn't seem to cover this

This function is very useful because we only need to specify an FX: ID, which can be used for controller, CSS and unit testing using test FX

Can I use this method, or do I make assumptions about unrecorded behavior that may change in future versions? Or is it recorded somewhere I missed?

Solution

IMO has some records in introduction to fxml:: variable resolution But it doesn't seem so obvious at first:

It should be completed with "if object defines idproperty and has not been set..." Based on the relevant source code, fxmlloader is about 708:

// Add the value to the namespace
        if (fx_id != null) {
            namespace.put(fx_id,value);

            // If the value defines an ID property,set it
            IDProperty idProperty = value.getClass().getAnnotation(IDProperty.class);

            if (idProperty != null) {
                Map<String,Object> properties = getProperties();
                // set fx:id property value to Node.id only if Node.id was not
                // already set when processing start element attributes
                if (properties.get(idProperty.value()) == null) {
                    properties.put(idProperty.value(),fx_id);
                }
            }
            ...
        }

I think it is possible to establish such behavior

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>