Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to evenly distribute elements of a JavaFX VBox

Im working with a VBox and added three labels to it. The vertical space between the labels is set with setSpacing() method. But it is a fixed value, say 20. if i change the value to 50, the space will be increased. But it is hard coding.

I want to know whether there is any pre-defined method that i can use so that the labels will be distributed evenly (based on the VBox height). if i resize the window, the labels (i mean, the space between the labels) should be adjusted so that they still are distributed evenly (with same space between any two labels).

As the VBox is set at left side of a BorderPane, i want "lab0" at "Display Line 1", "lab1" at "Display Line 5" and "lab2" at "Display Line 9" (in terms of alignment). In case more TextFields are added, "lab2" should be at last "Display Line X".

my code is here...please mark.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MyJavaFx extends Application
{
    public MyJavaFx()
    {
    }

    public void start(Stage primaryStage)
    {
        BorderPane pane = new BorderPane();
        pane.setTop(getRadioButtons());
        pane.setBottom(getPushButtons());
        pane.setLeft(getLeftLabels());
        pane.setRight(getRightLabels());
        pane.setCenter(getTextFields());
        Scene scene=new Scene(pane,1000,800);

        primaryStage.setTitle("Even");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String arg[])
    {
        Application.launch(arg);
    }

    FlowPane getRadioButtons()
    {
        FlowPane pane = new FlowPane();
        pane.setPadding(new Insets(10,10,10,10));
        pane.setHgap(10);
        RadioButton rb[]=new RadioButton[3];
        ToggleGroup radioGroup = new ToggleGroup();
        for(int i=0;i<rb.length;i++)
        {
            rb[i] = new RadioButton("Flag "+(i+1));
            rb[i].setToggleGroup(radioGroup);
            pane.getChildren().add(rb[i]);
        }
        rb[0].setSelected(true);
        pane.setAlignment(Pos.CENTER);
        return pane;
    }

    FlowPane getPushButtons()
    {
        FlowPane pane = new FlowPane();
        pane.setPadding(new Insets(10,30,30,10));
        pane.setHgap(10);
        Button rb[]=new Button[3];
        for(int i=0;i<rb.length;i++)
        {
            rb[i] = new Button("but"+i);
            pane.getChildren().add(rb[i]);
        }
        pane.setAlignment(Pos.BOTTOM_RIGHT);
        return pane;
    }
    VBox getLeftLabels()
    {
        VBox pane = new VBox();
        pane.setPadding(new Insets(10,10,10,10));
        pane.setSpacing(20);
        Label ll[]=new Label[3];
        for(int i=0;i<ll.length;i++)
        {
            ll[i] = new Label("lab"+i);
            pane.getChildren().add(ll[i]);
        }
        ll[0].setAlignment(Pos.TOP_LEFT);
        ll[1].setAlignment(Pos.CENTER_LEFT);
        ll[2].setAlignment(Pos.BOTTOM_LEFT);
        pane.setAlignment(Pos.CENTER_LEFT);
        return pane;
    }

    VBox getRightLabels()
    {
        VBox pane = new VBox();
        pane.setPadding(new Insets(10,10,10,10));
        pane.setSpacing(20);
        Label rb[]=new Label[3];
        for(int i=0;i<rb.length;i++)
        {
            rb[i] = new Label("lab"+i);
            pane.getChildren().add(rb[i]);
        }
        pane.setAlignment(Pos.CENTER_RIGHT);
        return pane;
    }

    VBox getTextFields()
    {
        VBox pane = new VBox();
        pane.setPadding(new Insets(10,10,10,10));
        pane.setSpacing(2);
        TextField tf[]=new TextField[9];
        for(int i=0;i<tf.length;i++)
        {
            tf[i] = new TextField("Display Line "+(i+1));

            pane.getChildren().add(tf[i]);
        }
        pane.setAlignment(Pos.CENTER_RIGHT);
        return pane;
    }
}
like image 262
PoornaChandra Avatar asked Nov 30 '16 08:11

PoornaChandra


People also ask

What is the difference between HBox and VBox pane?

HBox is a subclass of Pane that lays out its children next to each other, in a horizontal row. VBox is a subclass of Pane that lays out its children in vertical column. An HBox might be used as the bottom component in a BorderPane, making it possible to line up several components along the bottom of the border pane.

How does VBox work JavaFX?

VBox is a part of JavaFX. VBox lays out its children in form of vertical columns. If the vbox has a border and/or padding set, then the contents will be layed out within those insets. VBox class extends Pane class.

What is HBox in JavaFX?

The JavaFX HBox component is a layout component which positions all its child nodes (components) in a horizontal row. The Java HBox component is represented by the class javafx. scene.

What is StackPane in JavaFX?

StackPane lays out its children in a back-to-front stack. The z-order of the children is defined by the order of the children list with the 0th child being the bottom and last child on top. If a border and/or padding have been set, the children will be layed out within those insets.


1 Answers

What you could do is to add "spacers" before and after each label that will always grow or shrink according to the available space thanks to VBox.setVgrow(Node child, Priority value) with Priority.ALWAYS as priority value.

Here is the method that create a "spacer"

private Node createSpacer() {
    final Region spacer = new Region();
    // Make it always grow or shrink according to the available space
    VBox.setVgrow(spacer, Priority.ALWAYS);
    return spacer;
}

Then your code will be:

VBox getTextFields()
{
    VBox pane = new VBox();
    pane.setPadding(new Insets(10,10,10,10));
    TextField tf[]=new TextField[9];
    // Add first spacer
    pane.getChildren().add(createSpacer());
    for(int i=0;i<tf.length;i++)
    {
        tf[i] = new TextField("Display Line "+(i+1));
        pane.getChildren().add(tf[i]);
        // Add a spacer after the label
        pane.getChildren().add(createSpacer());
    }
    pane.setAlignment(Pos.CENTER_RIGHT);
    return pane;
}

About the second part of your question related to the fact that you want to align the labels labX with Display Line X, the simplest way would be to put all the labels that should be in the same row in a common HBox seperated with "spacers" as described above

like image 120
Nicolas Filotto Avatar answered Sep 19 '22 19:09

Nicolas Filotto