Skip to content
Related Articles

Related Articles

Improve Article

JavaFX | ProgressBar

  • Last Updated : 21 Oct, 2019

ProgressBar is a part of JavaFX package . It is a specialization of the ProgressIndicator which is represented as a horizontal bar. The progress bar usually shows the amount of completion of a task .

Constructor of the ProgressBar Class are:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  1. ProgressBar(): creates a new intermediate progress bar.
  2. ProgressBar(double p): creates a progressbar with a specified progress.

Commonly used methods:

methodexplanation
isIndeterminate()Gets the value of the property indeterminate.
getProgress()Gets the value of the property progress.
setProgress(double v)Sets the value of the property progress

Below program illustrate the ProgressBar:



This program creates a progress bar indicated by the name pb. The progress indicator will be created inside a scene, which in turn will be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage. Then a tile pane is created, on which addChildren() method is called to attach the progress indicator and the button inside the scene, along with the resolution specified by (200, 200) in the code. Finally the show() method is called to display the final results.




// Java program to illustrate the use of progressbar
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.*;
public class progress extends Application {
  
    static double ii = 0;
  
    // launch the application
    public void start(Stage s) throws Exception
    {
        // set title for the stage
        s.setTitle("creating progressbar");
  
        // create a progressbar
        ProgressBar pb = new ProgressBar();
  
        // create a tile pane
        TilePane r = new TilePane();
  
        // action event
        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                // set progress to different level of progressbar
                ii += 0.1;
                pb.setProgress(ii);
            }
  
        };
  
        // creating button
        Button b = new Button("increase");
  
        // set on action
        b.setOnAction(event);
  
        // add button
        r.getChildren().add(pb);
        r.getChildren().add(b);
  
        // create a scene
        Scene sc = new Scene(r, 200, 200);
  
        // set the scene
        s.setScene(sc);
  
        s.show();
    }
  
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}

Output:


Note: The following Programs might not run in an online IDE please use an offline Compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ProgressBar.html




My Personal Notes arrow_drop_up
Recommended Articles
Page :