How can i use a control more than one time in javafx 8?

为君一笑 提交于 2019-12-01 22:08:30

问题


I'm currently getting started with javafx 8 and came up with the following problem in a simple solution:

I've different controls (Button), which shall appear

  1. In the main content (center of a Pane)
  2. In a footer (bottom of a Pane)

Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");

VBox vbox = new VBox();
vbox.getChildren().addAll(one, two, three);

HBox hbox = new HBox();
hbox.getChildren().addAll(two, three); //To clarify my problem i leave one node in vbox

Now it appears to happen that the last .addAll(), deletes the references in the other box.

BorderPane root = new BorderPane();
root.setCenter(vbox);
root.setBottom(hbox);

Output:

I tried (for testing) to simply reuse a button, but:

root.setCenter(one);
root.setBottom(one);

results in

java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.RuntimeException: Exception in Application start method
...
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@1784a61

That made me think about the following questions:

  • Is there a way to work around that problem, except creating new button instances?
  • What happens to the HBox and VBox nodes?
  • Why controls can't be reused?

回答1:


As said in the JavaDocs of the Node class:

A node may occur at most once anywhere in the scene graph. Specifically, a node must appear no more than once in all of the following: as the root node of a Scene, the children ObservableList of a Parent, or as the clip of a Node.

If a program adds a child node to a Parent (including Group, Region, etc.) and that node is already a child of a different Parent or the root of a Scene, the node is automatically (and silently) removed from its former parent.

Therefore, you can't do what you're trying to do. One button can only be shown once, you can't have the same button at two places. To make this more clear - what should e.g. the getParent() method return if you were able to have the same instance at two places? Nothing, it's impossible. One instance can only exist at one place.

You must copy the button if you want to reuse it.




回答2:


In JavaFX nodes can only be used one time in the scene graph. This makes sence because a node, e.g., contains a location. If you would use it twice you would need two locations.




回答3:


The error you are getting

Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@1784a6

and that the scene shows the "one" button in the vbox and "two" and "three" in the hbox, is related. You declared only 3 buttons and the scene can only show 3 buttons. As per my comment, you need to declare button four and five and add those to the hbox and probably you will get to see all 5 buttons.

I don't know exactly why it does it like that, but it has to do with the initialization of the controls. The result could also have been that it added 3 buttons to the vbox and none to the hbox. But because the hbox is initialized after the vbox, is why it puts button 2 and 3 in the vbox and discards them in the hbox.( or actually throws an exception)



来源:https://stackoverflow.com/questions/29537491/how-can-i-use-a-control-more-than-one-time-in-javafx-8

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