Welcome guys. Learn the JavaFX UI widgets using these code snippets.
Example 1: Stage
Study the following 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:
AutoShutDownExample.java
FullScreenStageExample.java
MultipleStagesExample.java
MultipleStagesModalExample.java
StageDecorationExample.java
StageExample.java
StageKeyboardEventsExample.java
- In your editor or IDE, create a file known as
AutoShutDownExample.java
. - Then add the following code:
(a). AutoShutDownExample.java
We will need functionalities injected into this class via ready made classes. But first we have to import them. Let’s go ahead and do just that:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
Through inheritance we will be able to derive properties from a parent class
. However we have to extend that parent class
. So we do that using the extends
keyword.
public class AutoShutDownExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage) throws InterruptedException
We will add a main method to our Java class as shown below:
public static void main(String[] args) {
In this particular class
we will be overriding our void start(Stage primaryStage) throws InterruptedException
method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:
@Override
public void start(Stage primaryStage) throws InterruptedException {
Label label = new Label("This app will shut down automatically");
VBox vbox = new VBox(label);
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.setWidth(300);
primaryStage.setHeight(100);
primaryStage.show();
new Thread( () -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Here is the full code:
package com.jenkov.javafx.stage;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AutoShutDownExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws InterruptedException {
Label label = new Label("This app will shut down automatically");
VBox vbox = new VBox(label);
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.setWidth(300);
primaryStage.setHeight(100);
primaryStage.show();
new Thread( () -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(() -> {
primaryStage.close();
});
}).start();
//Thread.sleep(5000);
//primaryStage.close();
}
}
- Next create another file known as
FullScreenStageExample.java
. - And add the following code:
(b). FullScreenStageExample.java
So guys let;s go ahead and add some imports into our project. We add imports using the import
keyword:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
The next step is to extend our class
as shown below:
public class FullScreenStageExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
For you to be able to execute this class you need to assign it a main method using the following definition:
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) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
//Notice that the Stage title is not visible in full screen mode
primaryStage.setTitle("Full Screen Window Title");
primaryStage.setScene(scene);
primaryStage.setFullScreen(true);
primaryStage.show();
}
Here is the full code:
package com.jenkov.javafx.stage;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* Shows a Stage in full screen mode. Use ESC key to exit full screen mode.
*/
public class FullScreenStageExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
//Notice that the Stage title is not visible in full screen mode
primaryStage.setTitle("Full Screen Window Title");
primaryStage.setScene(scene);
primaryStage.setFullScreen(true);
primaryStage.show();
}
}
- Next create another file known as
MultipleStagesExample.java
. - And add the following code:
(c). MultipleStagesExample.java
So guys let;s go ahead and add some imports into our project. We add imports using the import
keyword:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
The next step is to extend our class
as shown below:
public class MultipleStagesExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
For you to be able to execute this class you need to assign it a main method using the following definition:
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) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
primaryStage.setTitle("JavaFX App");
primaryStage.setHeight(200);
primaryStage.setWidth(200);
primaryStage.setScene(scene);
primaryStage.show();
Stage stage = new Stage();
stage.setTitle("JavaFX Stage Window Title");
stage.setX(500);
stage.setY(500);
stage.setWidth(600);
stage.setHeight(300);
stage.showAndWait();
}
Here is the full code:
package com.jenkov.javafx.stage;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* This example shows two Stages at the same time.
* Please keep in mind that the second Stage is not shown in modal mode - meaning the second Stage
* does not block access to the first Stage.
*
*/
public class MultipleStagesExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
primaryStage.setTitle("JavaFX App");
primaryStage.setHeight(200);
primaryStage.setWidth(200);
primaryStage.setScene(scene);
primaryStage.show();
Stage stage = new Stage();
stage.setTitle("JavaFX Stage Window Title");
stage.setX(500);
stage.setY(500);
stage.setWidth(600);
stage.setHeight(300);
stage.showAndWait();
}
}
- Next create another file known as
MultipleStagesModalExample.java
. - And add the following code:
(d). MultipleStagesModalExample.java
So guys let;s go ahead and add some imports into our project. We add imports using the import
keyword:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
The next step is to extend our class
as shown below:
public class MultipleStagesModalExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
For you to be able to execute this class you need to assign it a main method using the following definition:
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 button = new Button("Click me if you can...");
button.setOnAction((event) -> {
System.out.println("Button clicked");
});
VBox vbox = new VBox(button);
Scene scene = new Scene(vbox);
primaryStage.setTitle("Primary Stage");
primaryStage.setX(250);
primaryStage.setY(250);
primaryStage.setHeight(500);
primaryStage.setWidth(500);
primaryStage.setScene(scene);
primaryStage.show();
Stage stage = new Stage();
stage.setTitle("Secondary Stage - in Modal Mode");
stage.setX(300);
stage.setY(300);
stage.setWidth(300);
stage.setHeight(300);
stage.initOwner(primaryStage);
//stage.initModality(Modality.NONE);
stage.initModality(Modality.WINDOW_MODAL);
//stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
}
Here is the full code:
package com.jenkov.javafx.stage;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
* This example shows two Stages at the same time - in modal mode.
* This means, that the second Stage blocks access to the first while the second Stage is visible/open.
* Notice how you cannot click the Button that is visible in the primary Stage while the secondary Stage is
* open.
*
*/
public class MultipleStagesModalExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Button button = new Button("Click me if you can...");
button.setOnAction((event) -> {
System.out.println("Button clicked");
});
VBox vbox = new VBox(button);
Scene scene = new Scene(vbox);
primaryStage.setTitle("Primary Stage");
primaryStage.setX(250);
primaryStage.setY(250);
primaryStage.setHeight(500);
primaryStage.setWidth(500);
primaryStage.setScene(scene);
primaryStage.show();
Stage stage = new Stage();
stage.setTitle("Secondary Stage - in Modal Mode");
stage.setX(300);
stage.setY(300);
stage.setWidth(300);
stage.setHeight(300);
stage.initOwner(primaryStage);
//stage.initModality(Modality.NONE);
stage.initModality(Modality.WINDOW_MODAL);
//stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
}
}
- Next create another file known as
StageDecorationExample.java
. - And add the following code:
(e). StageDecorationExample.java
So guys let;s go ahead and add some imports into our project. We add imports using the import
keyword:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
The next step is to extend our class
as shown below:
public class StageDecorationExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
For you to be able to execute this class you need to assign it a main method using the following definition:
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) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
primaryStage.setTitle("This title is not visible");
primaryStage.setScene(scene);
//primaryStage.initStyle(StageStyle.DECORATED);
primaryStage.initStyle(StageStyle.UNDECORATED);
//primaryStage.initStyle(StageStyle.TRANSPARENT);
//primaryStage.initStyle(StageStyle.UNIFIED);
//primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.show();
}
Here is the full code:
package com.jenkov.javafx.stage;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
* This example displays a Stage without any decorations - meaning without any "close" or "minimize" buttons,
* and no top title bar. You can play with the other Stage styles by commenting the other options in and out.
*
*/
public class StageDecorationExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
primaryStage.setTitle("This title is not visible");
primaryStage.setScene(scene);
//primaryStage.initStyle(StageStyle.DECORATED);
primaryStage.initStyle(StageStyle.UNDECORATED);
//primaryStage.initStyle(StageStyle.TRANSPARENT);
//primaryStage.initStyle(StageStyle.UNIFIED);
//primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.show();
}
}
- Next create another file known as
StageExample.java
. - And add the following code:
(f). StageExample.java
So guys let;s go ahead and add some imports into our project. We add imports using the import
keyword:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
The next step is to extend our class
as shown below:
public class StageExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
For you to be able to execute this class you need to assign it a main method using the following definition:
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) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
primaryStage.setTitle("Window Title");
primaryStage.setX(250);
primaryStage.setY(250);
primaryStage.setHeight(200);
primaryStage.setWidth(200);
primaryStage.setScene(scene);
primaryStage.show();
}
Here is the full code:
package com.jenkov.javafx.stage;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* Shows a simple Stage with a title, X,Y position and width and height.
*/
public class StageExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
primaryStage.setTitle("Window Title");
primaryStage.setX(250);
primaryStage.setY(250);
primaryStage.setHeight(200);
primaryStage.setWidth(200);
primaryStage.setScene(scene);
primaryStage.show();
}
}
- Next create another file known as
StageKeyboardEventsExample.java
. - And add the following code:
(g). StageKeyboardEventsExample.java
So guys let;s go ahead and add some imports into our project. We add imports using the import
keyword:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
Through inheritance we will be able to derive properties from a parent class
. However we have to extend that parent class
. So we do that using the extends
keyword.
public class StageKeyboardEventsExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage) throws InterruptedException
For our Java program to run we need a main method. Add it as shown below:
public static void main(String[] args) {
In this particular class
we will be overriding our void start(Stage primaryStage) throws InterruptedException
method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:
@Override
public void start(Stage primaryStage) throws InterruptedException {
Label label = new Label("This app will shut down automatically");
VBox vbox = new VBox(label);
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.setWidth(300);
primaryStage.setHeight(100);
primaryStage.show();
primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, (event) -> {
System.out.println("Key pressed: " + event.toString());
switch(event.getCode().getCode()) {
case 27 : { // 27 = ESC key
primaryStage.close();
break;
}
Here is the full code:
package com.jenkov.javafx.stage;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class StageKeyboardEventsExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws InterruptedException {
Label label = new Label("This app will shut down automatically");
VBox vbox = new VBox(label);
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.setWidth(300);
primaryStage.setHeight(100);
primaryStage.show();
primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, (event) -> {
System.out.println("Key pressed: " + event.toString());
switch(event.getCode().getCode()) {
case 27 : { // 27 = ESC key
primaryStage.close();
break;
}
case 10 : { // 10 = Return
primaryStage.setWidth( primaryStage.getWidth() * 2);
}
default: {
System.out.println("Unrecognized key");
}
}
});
}
}
Download
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |