CREW
CREW

Reputation: 936

How can I align my objects AND set spacing between each other? Programming in JavaFX and Java

I have been playing around with JavaFX and I really like it. But I can't seem to get my content where I want it. Even if I place a vbox on the CENTER and a label on the TOP, they're still in the center and almost touching each other.

I want to have (Centered in the screen of course):

My Title


SubLabel
Button1
Button2
Button3


BottomLabel

But it shows up as:

My Title
SubLabel
Button1
Button2
Button3
BottomLabel

If I settranslateX my vbox or top label, it moves the label, but also moves everything else with it.

How can I get it to align correctly?

Here is my pseudo-code:

private Node PreMenu()
    {

        Group group2 = new Group();

        BorderPane pane = new BorderPane();

        Text label = new Text("Please Choose a Option");
        label.setFont(Font.font("Kozuka Gothic Pro", 30));
        label.setEffect(addEffect(Color.web("#FF6600"), .85, 20));
        label.setTextAlignment(TextAlignment.CENTER);
        label.setTranslateY(-300); //This moves my label, but it also moves the vbox under it DOWN.

        VBox vbox = new VBox();

        Button option1= new Button("Firstbutton");
        Button option2= new Button("Secondbutton");
        Button option3= new Button("HelpButton");
        option1.setEffect(addEffect(Color.web("#FF6600"), .8, 10));
        option2.setEffect(addEffect(Color.web("#FF6600"), .8, 10));
        option3.setEffect(addEffect(Color.web("#FF6600"), .8, 10));
        option1.setTextAlignment(TextAlignment.CENTER);
        option1.setMinWidth(400);
        option2.setTextAlignment(TextAlignment.CENTER);
        option2.setMinWidth(400);
        option3.setTextAlignment(TextAlignment.CENTER);
        option3.setMinWidth(400);

        vbox.setSpacing(20);
        vbox.getChildren().add(option1);
        vbox.getChildren().add(option2);
        vbox.getChildren().add(option3);

        pane.setTop(label);
        pane.setCenter(vbox);

        group2.getChildren().add(pane);

        return group2;
    }

Another Node I have in my program with the same issue:

Upvotes: 0

Views: 2785

Answers (2)

Sergey Grinev
Sergey Grinev

Reputation: 34528

Easiest way with your code would be to set top and bottom padding for your Vbox with buttons:

vbox.setPadding(new Insets(50,0,50,0));

This line adds spacing of 50 pixels above and below vbox.

Upvotes: 2

john16384
john16384

Reputation: 8064

Don't put it in a Group. Just return the BorderPane directly. If you can post a fully working example (with a main()) I'll take a look at it, but I suspect the problem is the Group you are unnecessarily wrapping around the BorderPane.

Groups have a special function in JavaFX, read the documentation on the difference between Groups and Regions.

Upvotes: 1

Related Questions