如何将子组件和新属性添加到自定义JavaFX控件? [英] How to add children component and new attribute to custom JavaFX control?

查看:494
本文介绍了如何将子组件和新属性添加到自定义JavaFX控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个新的JavaFX组件并在FXML中使用它。如何定义此组件的子代用法和类型的可能性?有没有办法为此组件创建我自己的属性?例如,这就是我想要得到的:

I want to create a new JavaFX component and use it in FXML. How can I define the possibility of usage and types of children for this component? And is there a way to create my own atrributes for this component? As example, this is what I want to get:

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


推荐答案

设置属性值或指定FXML中的子值,您基本上只是在对象上设置属性。通常,属性用于简单的标量值,而嵌套值用于设置更复杂的值。

When you set the value of an attribute or specify a child value in FXML you are basically just setting properties on the object. Usually attributes are used for simple scalar values and nesting values is used for setting more complex values.

当FXML解析器遇到小写的属性或标记时,它会假定值是一个属性,并将尝试在特定对象上进行设置。

When the FXML parser encounters a lower case attribute or tag it assumes the value is a property and will try to set it on the specific object.

例如,考虑一个从 Parent 类并覆盖 getChildren()方法,使其成为控件公共API的一部分。

As an example consider a custom class that derives from the Parent class and overrides the getChildren() method so that it is part of the controls 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 your custom control in FXML like this.

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

从概念上讲,它类似于以下Java代码:

Which is conceptually similar to the following Java code:

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

您可以通过使用 DefaultProperty 注释如下:

You can improve this further by decorating your custom control class with the DefaultProperty annotation like this:

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

这可以省略< Children>< / Children> 标记,以便您的FXML可以更新为如下所示。

This allows you to omit the <Children></Children> tags so your FXML can be updated to look like this.

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

请注意,这会将控件添加到场景图中,但实际上可能不会在所需位置显示该控件到您还没有告诉它怎么做(您必须提供一些布局方法的实现,或者从已经具有布局逻辑的类(如 VBox )。

Note that this adds the control to the scene graph but may not actually display the control where you want it to as you have not told it how to do so (you will have to provide an implementation for some of the layout methods or derive from a class that already has layout logic such as VBox).

类似地,可以这样定义属性。

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...
    }
}

然后在您的FXML中:

And then in your FXML:

<MyNewComponent specificAttribute = "ABC123">

在概念上类似于:

myNewComponent.setSpecificAttribute("ABC123");

请参考官方 FXML参考以获得更多详细信息,因为根据您尝试使用的值类型,上述建议有一些例外进行设置,并可能需要一些其他代码才能使其正常工作。

Please refer to the official FXML reference for further details as there are some exceptions to the above advice depending on the type of value you are trying to set and may require some additional code to get it working.

这篇关于如何将子组件和新属性添加到自定义JavaFX控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆