How can i use a control more than one time in java

2020-04-11 07:42发布

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)

concept

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:

gui

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?

标签: java javafx-8
3条回答
放我归山
2楼-- · 2020-04-11 08:18

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.

查看更多
我只想做你的唯一
3楼-- · 2020-04-11 08:26

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)

查看更多
虎瘦雄心在
4楼-- · 2020-04-11 08:33

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.

查看更多
登录 后发表回答