ciprianr
ciprianr

Reputation: 309

JAVAFX - How can i add a button at a certain position when pressing another button?

I am new to javafx so i need a bit of help. I have 2 buttons in a VBox and i would like to add another button between these two buttons using only java code, how can this be done?

Any help is welcome.

Upvotes: 0

Views: 325

Answers (2)

Jurgen
Jurgen

Reputation: 2154

Alternatively you can already have the button in place but not visible:

button2.setVisible( false );
button2.setManaged( false );

In FMXL, it would be:

<Button fx:id="button2" visible="false" managed="false" />

The setManaged method will prevent the layout from making space for the button.

Then when you want the button to be revealed you do the following:

button2.setManaged( true );
button2.setVisible( true );

Upvotes: 1

Alexandre
Alexandre

Reputation: 4602

You'll want to use the VBox.getChildren().Add() method.

vbox.getChildren().add(1, button);

The first parameter allows you to insert the button at a certain index, if you want the button to be between the first and second element, simply insert at index 1.

Upvotes: 1

Related Questions