FXML,替代用途:使用ObservableList. [英] FXML, Alternate Uses: Using ObservableList<>

查看:186
本文介绍了FXML,替代用途:使用ObservableList.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此线程 FXML:替代用法中,我正在构建一个使用FXML进行加载的应用程序对象直接进入内存.

As in this thread FXML: Alternate Uses, I am building an application that uses FXML for loading objects into memory directly.

我已经成功设置了对象,可以轻松设置诸如hostNametypeNamemoto之类的特定属性,例如但是,在FXML中,您具有列表-就像ObservableList中保存的JavaFX UI元素的子节点一样.我在FXML中使用features(ObservableList)属性时遇到麻烦.给出错误UnsupportedOperationException: cannot determine type for property.如果它可以为BorderPane之类的其他对象提供支持,为什么它不能向列表中添加元素.

I have successfully setup objects where specific properties can be easily set like hostName, typeName, moto, e.g. But, in FXML you have lists - like child nodes of JavaFX UI elements held in a ObservableList. I am having trouble using a features (ObservableList) property in FXML. It gives an error that UnsupportedOperationException: cannot determine type for property. Why isn't it able to add elements to the list if it can for other objects like BorderPane.

推荐答案

由于ObservableList没有公共的具体类型,因此必须使用FXCollections工厂方法来获取实例.幸运的是,FXML旨在处理以下情况:通过使用fx:factory属性.您可以在

Because ObservableList has no public concrete types you have to use FXCollections factory methods to obtain an instance. Luckily, FXML was designed to handle cases like this: by using the fx:factory attribute. You can learn more about the different fx: options in Introduction to FXML | JavaFX 10.

一个例子.

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.collections.FXCollections?>

<FXCollections fx:factory="observableArrayList" xmlns="http://javafx.com/javafx/10.0.2"
            xmlns:fx="http://javafx.com/fxml/1">

    <!-- your children elements -->

</FXCollections>

元素的类型将是由fx:factory声明的工厂方法返回的类型.在这种情况下,是ObservableList(由ArrayList支持).

The type of the element will be the type returned by the factory method declared by fx:factory. In this case, an ObservableList (backed by an ArrayList).

如果您有一个用ObservableList字段(例如features)定义的类,并且带有适当的getter,则应该可以这样做:

If you have a class defined with an ObservableList field (such as features) with an appropriate getter you should be able to do this:

<YourObject>
    <features>
        <!-- your feature elements -->
    </features>
</YourObject>

但是,我很难使它正常工作.当我尝试它时,只有第一个元素被添加到ObservableList,我不知道为什么(也许是一个错误).

However, I am having trouble getting that to work. When I tried it only the first element was added to the ObservableList and I have no idea why (maybe it's a bug).

我设法使用以下方法解决此问题:

I managed to workaround this using:

<fx:define>
    <ArrayList fx:id="tempList">
        <!--- your feature elements -->
    </ArrayList>
</fx:define>

<YourObject>
    <features>
        <fx:reference source="tempList"/>
    </features>
</YourObject>

使用 注释也出于某种原因.它一直告诉我没有默认属性" ...但是它确实存在!

I was also having issues using the DefaultProperty annotation as well for some reason. It kept telling me "doesn't have a default property"... but it does!

除了替代方法",您可以像其他任何属性一样简单地将其设置" ObservableList.或者,您可以(可能)结合使用FXML注释和fx:id.

Other than the "workaround" you could simply have it "set" the ObservableList just like any other property. Or you could (probably) use an FXML annotation in combination with fx:id.

这篇关于FXML,替代用途:使用ObservableList.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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