In this tutorial we will learn more JavaFX widgets usage via simple examples.
Example 1: Vbox
Let us look at the code
Step 1: Create Project
- Open your favorite Java IDE.
- In the menu go to
File --> Create New Project
.
Step 2: Dependencies
No dependencies are needed for this project.
Step 3: Write Code
Our code will comprise the following java files:
VBoxExample.java
- In your editor or IDE, create a file known as
VBoxExample.java
. - Then add the following code:
(a). VBoxExample.java
Like in every program we write, we will need to import functionalities into our project. Add the following imports:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
Extend the class
as shown below:
public class VBoxExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
Let's create a main method as shown below:
public static void main(String[] args) {
In this particular class
we will be overriding our void start(Stage primaryStage)
method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:
@Override
public void start(Stage primaryStage) {
Button button1 = new Button("Button 1");
button1.setMaxWidth(Double.MAX_VALUE);
button1.setMaxHeight(Double.MAX_VALUE);
Button button1_2 = new Button("Button 1.2");
VBox vbox1 = new VBox(button1, button1_2);
vbox1.setPrefHeight(100);
vbox1.setAlignment(Pos.CENTER_LEFT);
//VBox.setMargin(button1, new Insets(10, 10, 10, 10));
VBox.setVgrow(button1, Priority.ALWAYS);
vbox1.setFillWidth(true);
vbox1.setStyle("-fx-border-style: solid inside;");
VBox vbox2 = new VBox(new Button("Button 2"));
vbox2.setAlignment(Pos.CENTER);
VBox vbox3 = new VBox(new Button("Button 3"));
vbox3.setAlignment(Pos.CENTER_RIGHT);
Scene scene = new Scene(new VBox(vbox1, vbox2, vbox3));
primaryStage.setScene(scene);
primaryStage.setHeight(300);
primaryStage.setWidth(400);
primaryStage.show();
}
Here is the full code:
package com.jenkov.javafx.vbox;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class VBoxExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Button button1 = new Button("Button 1");
button1.setMaxWidth(Double.MAX_VALUE);
button1.setMaxHeight(Double.MAX_VALUE);
Button button1_2 = new Button("Button 1.2");
VBox vbox1 = new VBox(button1, button1_2);
vbox1.setPrefHeight(100);
vbox1.setAlignment(Pos.CENTER_LEFT);
//VBox.setMargin(button1, new Insets(10, 10, 10, 10));
VBox.setVgrow(button1, Priority.ALWAYS);
vbox1.setFillWidth(true);
vbox1.setStyle("-fx-border-style: solid inside;");
VBox vbox2 = new VBox(new Button("Button 2"));
vbox2.setAlignment(Pos.CENTER);
VBox vbox3 = new VBox(new Button("Button 3"));
vbox3.setAlignment(Pos.CENTER_RIGHT);
Scene scene = new Scene(new VBox(vbox1, vbox2, vbox3));
primaryStage.setScene(scene);
primaryStage.setHeight(300);
primaryStage.setWidth(400);
primaryStage.show();
}
}
Download
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |