简体   繁体   English

FXML,替代用法:使用ObservableList <>

[英]FXML, Alternate Uses: Using ObservableList<>

As in this thread FXML: Alternate Uses , I am building an application that uses FXML for loading objects into memory directly. 就像该线程FXML:Alternate Uses一样 ,我正在构建一个使用FXML将对象直接加载到内存中的应用程序。

I have successfully setup objects where specific properties can be easily set like hostName , typeName , moto , eg But, in FXML you have lists - like child nodes of JavaFX UI elements held in a ObservableList . 我已经成功设置了对象,可以在其中轻松设置特定属性,例如hostNametypeNamemoto 。例如,但是在FXML中,您具有列表-像ObservableList保存的JavaFX UI元素的子节点一样。 I am having trouble using a features ( ObservableList ) property in FXML. 我在FXML中使用featuresObservableList )属性时遇到麻烦。 It gives an error that UnsupportedOperationException: cannot determine type for property . 它给出了一个错误,即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 . 如果它可以为BorderPane等其他对象添加元素,为什么它不能向列表中添加元素。

Because ObservableList has no public concrete types you have to use FXCollections factory methods to obtain an instance. 由于ObservableList没有公共的具体类型,因此必须使用FXCollections工厂方法来获取实例。 Luckily, FXML was designed to handle cases like this: by using the fx:factory attribute. 幸运的是,FXML旨在处理以下情况:通过使用fx:factory属性。 You can learn more about the different fx: options in Introduction to FXML | 您可以了解有关不同fx:更多信息fx: FXML简介|中的选项。 JavaFX 10 . JavaFX 10

An example. 一个例子。

<?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>

The type of the element will be the type returned by the factory method declared by fx:factory . 元素的类型将是fx:factory声明的factory方法返回的类型。 In this case, an ObservableList (backed by an ArrayList ). 在这种情况下,将创建一个ObservableList (由ArrayList支持)。


If you have a class defined with an ObservableList field (such as features ) with an appropriate getter you should be able to do this: 如果您有一个使用ObservableList字段(例如features )定义的类,并且带有适当的getter,则您应该能够做到这一点:

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

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). 当我尝试它时,只有第一个元素被添加到ObservableList ,我也不知道为什么(也许是一个错误)。

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. 由于某些原因,我在使用DefaultProperty批注时也遇到了问题。 It kept telling me "doesn't have a default property"... but it does! 它一直告诉我“没有默认属性” ...但是它确实存在!

Other than the "workaround" you could simply have it "set" the ObservableList just like any other property. 除了“替代方法”,您可以像其他任何属性一样简单地将其“设置” ObservableList Or you could (probably) use an FXML annotation in combination with fx:id . 或者,您可以(可能)将FXML注释与fx:id结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM