JAVAFX LAYOUTS
Layout panes are containers that are used for flexible and dynamic arrangements of UI controls within a scene graph of a JavaFX application. As a window is resized, the layout pane automatically repositions and resizes the nodes it contains. JavaFX has the following built-in layout panes: Flow Pane, HBox, VBox, BorderPane, GridPane, StackPane, TilePane, AnchorPane.
1. JAVAFX FlowPane
Flow Pane positions nodes in a row or a column, where the nodes are wrapped when they all cannot be shown. The default orientation of a flow pane is horizontal. The class named FlowPane of the package javafx.scene.layout represents the Flow Pane layout. There are 8 constructors in the class that are given below.
•FlowPane()
• FlowPane (Double Hgap, Double Vgap)
• Flow Pane (Double Hgap, Double Vgap, Node? children)
• Flow Pane (Node... Children)
• Flow Pane (Orientation orientation)
• FlowPane(Orientation orientation, double Hgap, Double Vgap)
• Flow Pane(Orientation orientation, double Hgap, Double Vgap, Node children)
• Flow Pane(Orientation orientation, Node Children)
//Example: Program to demonstrate FlowPane Layout
import javafx. application. Application;
import static javafx. application. Application.launch;
import javafx.scene.control.Button; import javafx.stage.Stage;
import javafx.scene. Scene;
import javafx.scene.layout. FlowPane;
import javafx.geometry. Insets;
import javafx.geometry. Orientation; public class FlowPane Demo extends Application
{
public void start (Stage primaryStage) throws Exception
{
Button btn1=new Button("Button 1");
Button btn2-new Button("Button 2");
Button btn3=new Button("Button 3");
Button btn4=new Button("My Button 4");
Button btn5-new Button("This is Button 5");
FlowPane root = new FlowPane (Orientation. HORIZONTAL, 5, 5);
root.setPadding (new Insets (5));
root.getChildren().add(btn1);
root.getChildren().add(btn2);
root.getChildren().add(btn3);
root.getChildren().add(btn4);
root.getChildren().add(btn5);
Scene scene=new Scene (root, 300,200);
primaryStage.setScene (scene);
primaryStage.setTitle("JavaFX FlowPane Example");
}
primaryStage.show();
public static void main (String[] args)
{
launch(args);
}
}
2. JavaFX HBox
HBox layout pane arranges the nodes in a single row. It is represented by javafx.scene.layout.HBox class. We just need to instantiate HBox class in order to create HBox layout.
Constructors
The HBox class contains two constructors that are given below.
new HBox() : create HBox layout with 0 spacing
new Hbox(Double spacing) : create HBox layout with a spacing value
Example
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Label_Test extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Button btn1 = new Button("Button 1");
Button btn2 = new Button("Button 2");
HBox root = new HBox();
Scene scene = new Scene(root,200,200);
root.getChildren().addAll(btn1,btn2);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
3. JavaFX VBox
Instead of arranging the nodes in horizontal row, Vbox Layout Pane arranges the nodes in a single vertical column. It is represented by javafx.scene.layout.VBox class which provides all the methods to deal with the styling and the distance among the nodes. This class needs to be instantiated in order to implement VBox layout in our application.
Constructors
VBox() : creates layout with 0 spacing
Vbox(Double spacing) : creates layout with a spacing value of double type
Vbox(Double spacing, Node? children) : creates a layout with the specified spacing among the specified child nodes
Vbox(Node? children) : create a layout with the specified nodes having 0 spacing among them
Example
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Label_Test extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Button btn1 = new Button("Button 1");
Button btn2 = new Button("Button 2");
VBox root = new VBox();
Scene scene = new Scene(root,200,200);
root.getChildren().addAll(btn1,btn2);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
4. JavaFX BorderPane
BorderPane arranges the nodes at the left, right, centre, top and bottom of the screen. It is represented by javafx.scene.layout.BorderPane class. This class provides various methods like setRight(), setLeft(), setCenter(), setBottom() and setTop() which are used to set the position for the specified nodes. We need to instantiate BorderPane class to create the BorderPane layout.
Constructors
There are the following constructors in the class.
BorderPane() : create the empty layout
BorderPane(Node Center) : create the layout with the center node
BorderPane(Node Center, Node top, Node right, Node bottom, Node left) : create the layout with all the nodes
Example
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class Label_Test extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane BPane = new BorderPane();
BPane.setTop(new Label("This will be at the top"));
BPane.setLeft(new Label("This will be at the left"));
BPane.setRight(new Label("This will be at the Right"));
BPane.setCenter(new Label("This will be at the Centre"));
BPane.setBottom(new Label("This will be at the bottom"));
Scene scene = new Scene(BPane,600,400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
5. JavaFX StackPane
The StackPane layout pane places all the nodes into a single stack where every new node gets placed on the top of the previous node. It is represented by javafx.scene.layout.StackPane class. We just need to instantiate this class to implement StackPane layout into our application.
Constructors
The class contains two constructors that are given below.
StackPane()
StackPane(Node? Children)
Example
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Label_Test extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Button btn1 = new Button("Button 1 on bottom ");
Button btn2 = new Button("Button 2 on top");
StackPane root = new StackPane();
Scene scene = new Scene(root,200,200);
root.getChildren().addAll(btn1,btn2);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
6. JavaFX GridPane
GridPane Layout pane allows us to add the multiple nodes in multiple rows and columns. It is seen as a flexible grid of rows and columns where nodes can be placed in any cell of the grid. It is represented by javafx.scence.layout.GridPane class. We just need to instantiate this class to implement GridPane.
Constructors
The class contains only one constructor that is given below.
Public GridPane(): creates a gridpane with 0 hgap/vgap.
Example:
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Label_Test extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Label first_name=new Label("First Name");
Label last_name=new Label("Last Name");
TextField tf1=new TextField();
TextField tf2=new TextField();
Button Submit=new Button ("Submit");
GridPane root=new GridPane();
Scene scene = new Scene(root,400,200);
root.addRow(0, first_name,tf1);
root.addRow(1, last_name,tf2);
root.addRow(2, Submit);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Comments
Post a Comment