×

JavaFX Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used to specify how components are arranged and aligned. All the classes for setting the layout are present in the javafx.scene.layout package.

Different Layouts in JavaFX applications:

1. BorderPane:

The BorderPane layout in the JavaFX application is used to place the nodes in the left, right, top, bottom, and center of the screen.

2. FlowPane:

The FlowPane is used to arrange the nodes according to the horizontal spaces available. If the horizontal space is less than node width. It goes to the next line.

3. GridPane:

The GridPane is used to arrange the components in the form of rows and columns.

4. StackPane:

In the JavaFX application, nodes in the stack pane are arranged in the form of a stack.

5. HBox:

The HBox is used to arrange nodes in one row.

6. VBox:

 The VBox is used to arrange nodes in one column.

7. AnchorPane:

This layout is used to set nodes at a specified distance.

8. TilePane:

Tile pane is used to arrange all the nodes in the form of tiles in the container.

9. Pane:

All the layout classes have the base class, pane in the JavaFX application.

Creating Layout in JavaFX:

In order to create a layout we have to follow below steps:

  1. Create a node that is to be placed in the container.
  2. Instantiate the Layout class.
  3. Set required properties of the layout using different methods.
  4. Add the nodes in the layout.

Create a node that is to be placed in the container.

We have to first create the required nodes by using various controls in JavaFX classes.

Example- If you want One label to enter your name and one button submitted with the textfield to enter the name in the container, you will have to instantiate respective classes using JavaFX packages.

//Create the Label in JavaFX
Label lbl = new Label(“ Enter Your Name ”);       


//Create the text field in JavaFX
TextField textField = new TextField();       


//Create the button in JavaFX
Button btn = new Button(“ Submit ”); 

Instantiate the Layout class.

Once all the nodes are created using JavaFX application classes, We have to Instantiate the required Layout class using respective packages.

Example- If we want VBox in your application, we can instantiate by using the below statement,

HBox box = new HBox();

Set required properties of the layout using different methods.

Using various methods we can specify various properties of the layout. We can use setter methods for the same.

Example- We can set the spacing property of VBox by calling the setSpacing() method on the layout object.

box.setSpacing(30);

Add the nodes in the layout.

We need to add all objects of the shape to a specified group by passing it as the parameter to the constructor as written below.

Group root = new Group(label);

Example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;  


public class SimpleLayout extends Application
{  
@Override  
public void start(Stage primaryStage) throws Exception
{  
    Label newlb = new Label(" Label in JavaFX ");  
    StackPane pane = new StackPane();
    Scene scene = new Scene( pane, 500, 400 );  
    pane.getChildren().addAll(newlb);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle(" Label Example in JavaFX ");  
    primaryStage.show();  
}  


public static void main(String[] args) 
{  
    launch(args);  
      
}  
} 

Output:

In order to create layout and place component in it, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene, javafx.scene.control.Label, javafx.stage.Stage, and javafx.scene.layout.StackPane.

The java class is created which extends the Application class. Also start method is overridden with a primarystage object as an argument in it. In the start method label is created with text specified in it. Then StackPane container is created. An object of StackPane is passed to the Scene constructor as an argument. Then addAll() method is then added to the container. Using show() method on primaryStage is called to display container. Then in the main method launch(args) method is called.

Right-click on the java file, to run the application. Then select Run As, and choose Java application. It will show one container with the title "Label Example in JavaFX" and the Label in the center.

Layouts in JavaFX applications

Related Topics

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.

JavaFX CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

4 minutes read.

JavaFX Light Point

In the JavaFX application, in order to apply various LightPoint effect from a single source, we use this class. It has the ability to apply light from a single source...

4 minutes read.

JavaFX Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX Selectors

In the JavaFX application, in order to apply various css effect, we use the various css classes available in the JavaFX. This is generally used to enhance the appearance property...

4 minutes read.

JavaFX Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX Color Input

In the JavaFX application, in order to apply various color input effect, we use the ColorInput class. It has the ability to fill the specified shape with a given color...

4 minutes read.

JavaFX ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

JavaFX Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

4 minutes read.

JavaFX Translate Transition

In the JavaFX application, in order to generate the translate transition, we have to apply translate transition on any particular shape. It has the ability to translate the shape along...

3 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All...

4 minutes read.

JavaFX Light Spot

In the JavaFX application, in order to apply various Light Spot effect from a single source, we use this class. It has the ability to apply light from all the...

4 minutes read.

JavaFX Pause Transition

In the JavaFX application, in order to generate the pause transition, we have to apply pause transition on any particular shape. It has the ability to take a particular pause...

4 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX Cylinder

In the JavaFX application, in order to draw a Cylinder, the Cylinder class is used. The Cylinder allows us to form a 3D shape having rectangular faces and a straight...

4 minutes read.