我有一个名为“语言”的自定义类,我想在fxml中定义它,如:
<Language fx:value="getBundle"> </Language>语言直接扩展Object而不是任何JavaFX组件。 显然它抛出以下异常:
无法强制com.bl.commons.Language@69435到类javafx.scene.Node
那我该怎么做? 我想要的是,我的大部分模态都在该类中,我想直接调用Language方法并在fxml本身中设置内容。 如果这不可能那么任何替代方案?
I have a custom class called 'Language', I want to define it in fxml like:
<Language fx:value="getBundle"> </Language>Language extends Object directly and not any JavaFX component. Obviously it throws the below Exception:
Unable to coerce com.bl.commons.Language@69435 to class javafx.scene.Node
So how do I do it? What I want is that, most of my modal is in that class and I want to directly call methods of Language and set content in fxml itself. If this is not possible then any alternatives?
最满意答案
据我所知,你的类必须以某种方式扩展Node才能被添加到其他元素的子列表中(当你把它放在FXML的任何地方时你正在做的事情),因为那些子Node存储为ObservableList<Node> 。
请注意,这并不意味着你必须做任何特别的事情,只需这样做:
public class Language extends Node // or StackPane or whatever { public Language() { super(); // ...carry on... } }如果Language已经扩展了其他内容,那么您将希望该类扩展Node或重新考虑您的类层次结构。
As far as I know your class has to extend Node in some way in order to be added to the list of children of other elements (which is what you are doing when you put it anywhere in FXML), because those children are stored as ObservableList<Node>.
Note that this doesn't really mean you have to do anything special, just do:
public class Language extends Node // or StackPane or whatever { public Language() { super(); // ...carry on... } }If Language already extends something else you will want to make that class extend Node or rethink your class hierarchy.
更多推荐
发布评论