Updated March 18, 2023
Introduction to JavaFX ProgressBar
In the JavaFX package, ProgressBar is a form of Progress Indicator which is denoted as a horizontal bar. It helps in displaying the progress of a particular task. Normally, it is a double value between the range 0 and 1. The JavaFX Progress Bar is instantiated from the class javafx.scene.control.ProgressBar.
The methods, constructors, and examples of JavaFX ProgressBar will be discussed in the following sections.
JavaFX ProgressBar Constructors
The following are the two constructors for JavaFX ProgressBar Constructors.
1. ProgressBar(): An intermediate ProgressBar will be created .
// create a Progress
ProgressBar pb = new ProgressBar();
2. ProgressBar(double s): A ProgressBar will be created with a double s as its progress.
// create a ProgressBar
ProgressBar pb = new ProgressBar(0);
Methods of JavaFX ProgressBar
Let us see some of the commonly used methods.
- isIndeterminate(): Indeterminate property’s value will be returned.
- getProgress(): Progress property’s value will be returned.
- setProgress(boolean v): Progress property’s value will be set.
How to Create ProgressBar?
There are several steps to create a progress bar.
1. Set the Title for the Stage Created
s.setTitle("ProgressBar Sample");
2. create a Progress Bar: Progress bars can be parameterized or non-parameterized as follows.
// create a ProgressBar
ProgressBar pb = new ProgressBar(0); //parameterized
// create a ProgressBar
ProgressBar pb = new ProgressBar(); //non-parameterized
3. Add Progressbar Created to The Scene Graph: Add the progressbar to the scene graph using the below steps.
Scene sc = new Scene(r, 300, 200);
s.setScene(sc);
s.show();
Program to Implement JavaFX Progress Bar
Now let us see some of the JavaFX programs that implement JavaFX progressbar.
Program #1
Java program to demonstrate progress bar is given below:
Code:
//Java program to demonstrate progress bar
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
//sample class that extends application base class
public class JavaFXProgressBarExample extends Application {
//application starts here
public void start(Stage s) throws Exception {
//create stackpane
StackPane r = new StackPane();
//create progress bar
ProgressBar p = new ProgressBar();
r.getChildren().add(p);
//create scene
Scene sc = new Scene(r,400,300);
//set the scene
s.setScene(sc);
//set the title
s.setTitle("Sample Progress Bar");
//display the results
s.show();
}
//main method
public static void main(String[] args) {
launch(args);
}
}
Output:
Explanation:
- A progress bar is created firstly and added it to the scene graph.
- Once it is done, the output displayed a progressbar as shown above.
Program #2
Java program to display a progress bar with a label.
Code:
//Java program to demonstrate progress bar with a label
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
//sample class that extends application base class
public class JavaFXProgressBarExample extends Application {
//application starts here
public void start(Stage s) throws Exception {
// set title
s.setTitle("JavaFX Progress bar example");
//create progress bar
ProgressBar p = new ProgressBar();
// tile pane
TilePane tp = new TilePane();
// label
Label l = new Label(" This is the progress bar !!!!");
tp.getChildren().add(p);
tp.getChildren().add(l);
Scene sc = new Scene(tp, 200, 200);
s.setScene(sc);
s.show();
}
//main method
public static void main(String[] args) {
launch(args);
}
}
Output :
Explanation: The difference of this program from the above program is that, here, a label is used along with the progress bar.
Program #3
Java program to display progressbar with a particular value.
Code:
//Java program to demonstrate progress bar with a value mentioned by the user
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
//sample class that extends application base class
public class JavaFXProgressBarExample extends Application {
//application starts here
public void start(Stage s) throws Exception {
// set title
s.setTitle("JavaFX Progress Bar example");
//create progress bar
ProgressBar p = new ProgressBar(0);
//set a value for progress bar
p.setProgress(0.75);
// tile pane
TilePane tp = new TilePane();
// label
Label l = new Label(" Showing a progress of 75% !!!!");
tp.getChildren().add(p);
tp.getChildren().add(l);
Scene sc = new Scene(tp, 200, 200);
s.setScene(sc);
s.show();
}
//main method
public static void main(String[] args) {
launch(args);
}
}
Output:
Explanation:
- In the progress bar, the user can also set the value prior to the compiling of code.
- In this program, the value is set as 0.75 and it can be seen that the progress bar is displayed with 75% of the bar darkened.
- The darkened portion implies that the progress is 75%.
Program #4
Java program to demonstrate the progress bar with a button to seek the bar given below:
Code:
//Java program to demonstrate progress bar with a button to seek the bar
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import java.io.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import java.net.*;
//sample class that extends application base class
public class JavaFXProgressBarExample extends Application {
static double v =0;
//application starts here
public void start(Stage s) throws Exception {
// set title
s.setTitle("JavaFX Progress bar example");
//create progress bar
ProgressBar p = new ProgressBar(0);
// tile pane
TilePane tp = new TilePane();
// action event
EventHandler<ActionEvent> ev = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
// set progress to different level of progressbar
v += 0.1;
p.setProgress(v);
}
};
// creating button
Button btn = new Button("click me to move progress bar");
// set on action
btn.setOnAction(ev);
tp.getChildren().add(p);
tp.getChildren().add(btn);
//create the scene
Scene sc = new Scene(tp, 200, 200);
//set the scene
s.setScene(sc);
//display the result
s.show();
}
//main method
public static void main(String[] args) {
launch(args);
}
}
Output:
When the button is clicked, the progress gets increased as shown in the below figures.
Explanation:
- A progress bar and button are created at first.
- When the button is clicked, an action to increase the progress is set using an event handler.
- So, whenever the button is clicked, progress gets increased.
Conclusion
In JavaFX, a Progress bar is used to display the progress of a task using a horizontal bar. The decision to choose a parameterized or non-parameterized constructor depends on the user’s requirement. Here, Several methods and programs of the JavaFX progress bar are explained in detail as well.
Recommended Articles
This is a guide to JavaFX ProgressBar. Here we discuss the constructors, methods, and steps for creating JavaFX ProgressBar along with the various examples. You may also look at the following articles to learn more –