[英]How to add the same node multiple times, with different information? (JavaFX)
I'm making a program which notifies me when a new episode of one of my series is uploaded, I have a built-in notification center, which stores the uploads. 我正在制作一个程序,当上载一个系列的新剧集时通知我,我有一个内置的通知中心,用于存储上载内容。 How I did this, was basically making a VBox, the NotificationPanel, then having a nLayout with basic things like a close-button, and every time I there is a new episode uploaded, add the nLayout with different name and text for each serie.
我的操作方式基本上是制作一个VBox,NotificationPanel,然后使用一个具有诸如关闭按钮之类的基本功能的nLayout,并且每次我上传新剧集时,为每个意向添加具有不同名称和文本的nLayout。 But I've seen that this isn't "possible", because I get this error:
但是我看到这不是“可能的”,因为我得到了这个错误:
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = VBox@2168def7
Anyone knows how I can get this to work? 有人知道我如何使它工作吗?
You cannot add the same node multiple times. 您不能多次添加同一节点。 Just create a new instance of your NotificationPanel for every notification
只需为每个通知创建一个新的NotificationPanel实例
As comments say, you can't add the same instance of a class multiple times in your VBox. 正如评论所说,您不能在VBox中多次添加同一类的实例。
Each time you catch your event (episode uploaded) what you can do is : 每次您捕获事件(上载剧集)时,您可以做的是:
Btw, by doing this, you don't use the fancy Model-View binding mechanism of JavaFX. 顺便说一句,这样做,您不会使用JavaFX精美的Model-View绑定机制。 A better solution would be to have a model class with an ObservableList in which you could push an instance of an object made to contain the information of the uploaded episode, and bind this ObservableList to a ListView with a custom CellFactory to build the cells of your List the way you want.
更好的解决方案是使用带有ObservableList的模型类,在其中您可以推送对象的实例以包含上载情节的信息,然后将此ObservableList绑定到具有自定义CellFactory的ListView上以构建您的单元格列出您想要的方式。
You can find information about the binding mechanism here : http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm 您可以在此处找到有关绑定机制的信息: http : //docs.oracle.com/javafx/2/binding/jfxpub-binding.htm
You can find information about ListView and custom CellFactory here : http://docs.oracle.com/javafx/2/ui_controls/list-view.htm 您可以在此处找到有关ListView和自定义CellFactory的信息: http ://docs.oracle.com/javafx/2/ui_controls/list-view.htm
Hope it helps :) 希望能帮助到你 :)
If I get you correctly you should probably create your own custom Node. 如果我正确理解了您,则可能应该创建自己的自定义节点。 For example by
例如通过
public class EpisodeBox extends Vbox {
public EpisodeBox(String name){
getChildren().add(new Label(name));
getChildren().add(new Button("Close"));
// ... etc.
}
}
As Kwoinkwoin stated you can then on each event create a new EpisodeBox and add this one to your Panel. 正如Kwoinkwoin所说,您可以在每个事件上创建一个新的EpisodeBox并将其添加到面板中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.