How do I add subcomponents and new properties to a custom JavaFX control?

I want to create a new JavaFX component and use it in fxml How do I define the usage possibilities and subtypes of this component? Is there any way to create my own ATR ributes for this component? For example, this is what I want:

<MyNewComponent specificAttribute="...">
   <children>
      <SpecificChildren></SpecificChildren>
   </children>
</MyNewComponent>

Solution

When you set the value of an attribute or refer to a subvalue in fxml, you basically set the attribute on the object In general, attributes are used for simple scalar values and nested values are used to set more complex values

When the fxml parser encounters a lowercase attribute or tag, it assumes that the value is an attribute and will try to set it on a specific object

For example, consider a custom class derived from the parent class and override the getchildren () method to make it part of the control's public API

public class MyNewComponent extends Parent {

    @Override
    public ObservableList<Node> getChildren() {
        return super.getChildren();
    }
}

You should now be able to add other controls directly to custom controls in fxml

<MyNewComponent>
    <children>
        <SpecificChildren></SpecificChildren>
    <children>
</MyNewComponent>

This is conceptually similar to the following java code:

myNewComponent.getChildren().add(new SpecificChildren());

You can further improve this by decorating the custom control class with the defaultproperty annotation, as follows:

@DefaultProperty(value = "children")
public class MyNewComponent extends Parent {

This allows you to omit the < children > < / children > tag so that your fxml can be updated like this

<MyNewComponent>
    <SpecificChildren></SpecificChildren>
</MyNewComponent>

Note that this will add the control to the scene diagram, but it may not be able to display the control in the position you want because you have not told it how to do this (you must provide an implementation for some layout methods or from classes that already have layout logic, such as V@R_847_2419 @).

Similarly, attributes can be defined like this

@DefaultProperty(value = "children")
public class MyNewComponent extends Parent {

    @Override
    public ObservableList<Node> getChildren() {
        return super.getChildren();
    }

    public String setSpecificAttribute(String str) {
        // Do something here...
    }
}

Then in your fxml:

<MyNewComponent specificAttribute = "ABC123">

This is conceptually similar to:

myNewComponent.setSpecificAttribute("ABC123");

For more details, please refer to the official fxml reference, because there are some exceptions to the above recommendations, depending on the type of value you are trying to set, and some additional code may be required to make it work properly

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
分享
二维码
< <上一篇
下一篇>>