adding the same object twice

爷,独闯天下 提交于 2020-02-04 04:47:10

问题


i have an label and two panels. i want to add the label into each panel, but after rendering is shown only one label. i can create second label, but i think, it must be possible to add the same label twice. Here my code:

// Create labels
Label sectorLabel = new Label("Bereich");

// Create panels/rows
HorizontalPanel row1 = new HorizontalPanel();
HorizontalPanel row2 = new HorizontalPanel();

// Add content to row1
row1.add(sectorLabel);

// Add content to row2      
row2.add(sectorLabel);

回答1:


You can't add the same widget to two panels, a widget can have only one parent.

The solution is simply to create a second label with the same contents.




回答2:


When you add a widget as a child to another widget, the containing class will first do a removeFromParent operation. So no you can not add a widget twice.

Why would you want to do that ? Maybe that is the right question to ask. If you don't want to update the status on multiple locations on the screen then maybe you need to apply an observer pattern to your data and update all the observers when the data changes.



来源:https://stackoverflow.com/questions/2199574/adding-the-same-object-twice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!