A simple JavaFX tutorial through simple snippets.
Example 1: Concurrency
Here are 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:
ConcurrencyExample.java
- In your editor or IDE, create a file known as
ConcurrencyExample.java
. - Then add the following code:
(a). ConcurrencyExample.java
First, go ahead and add the following imports:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
We will need to extend our class using the extend
keyword. By doing that our class
can make use of inheritance to derive properties and functions defined in the parent class
.
public class ConcurrencyExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
void run()
void run()
Every Java Program must have a main method. This is the entry point of all Java applications include JavaFX. Add a main()
method and inside it invoke the launch()
function.
public static void main(String[] args) {
We have 3methods to override in this class. Here are those methods:
public void start(Stage primaryStage) {
public void run() {
public void run() {
Prepend the@Override
modifier to your method. Then add implementation code as follows:
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX App");
ProgressBar progressBar = new ProgressBar(0);
VBox vBox = new VBox(progressBar);
Scene scene = new Scene(vBox, 960, 600);
primaryStage.setScene(scene);
primaryStage.show();
Thread taskThread = new Thread(new Runnable() {
@Override
public void run() {
double progress = 0;
for(int i=0; i<10; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:
@Override
public void run() {
progressBar.setProgress(reportedProgress);
}
Here is the full code:
package com.jenkov.javafx.concurrency;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ConcurrencyExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX App");
ProgressBar progressBar = new ProgressBar(0);
VBox vBox = new VBox(progressBar);
Scene scene = new Scene(vBox, 960, 600);
primaryStage.setScene(scene);
primaryStage.show();
Thread taskThread = new Thread(new Runnable() {
@Override
public void run() {
double progress = 0;
for(int i=0; i<10; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progress += 0.1;
double reportedProgress = progress;
Platform.runLater(new Runnable() {
@Override
public void run() {
progressBar.setProgress(reportedProgress);
}
});
}
}
});
taskThread.start();
}
}
Download
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |