Saturday 23 June 2018

JavaFX: ProgressBar: Inderterminate progress bar

Sometimes application may not know, how long the execution takes. In those cases, you can set the progress bar to indeterminate state.

How to set the progress bar state to indeterminate?
If you create ProgressBar widget using default constructor, it will be in indeterminate state. You can also set the progress bar to indeterminate state by setting the progress to 'INDETERMINATE_PROGRESS'.

Example 1
ProgressBar indeterminateProgressBar1 = new ProgressBar();

Example 2
ProgressBar indeterminateProgressBar2 = new ProgressBar(0.3);
indeterminateProgressBar2.setProgress(ProgressBar.INDETERMINATE_PROGRESS);

Find the below working application.

ProgressBarApp.java
package com.sample.demos;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ProgressBarApp extends Application {

 @Override
 public void start(Stage primaryStage) throws Exception {
  ProgressBar indeterminateProgressBar1 = new ProgressBar();

  ProgressBar indeterminateProgressBar2 = new ProgressBar(0.3);
  indeterminateProgressBar2.setProgress(ProgressBar.INDETERMINATE_PROGRESS);

  VBox vBox = new VBox(20, indeterminateProgressBar1, indeterminateProgressBar2);

  Scene scene = new Scene(vBox);

  primaryStage.setScene(scene);

  primaryStage.setTitle("Progress Bar widget Example");
  primaryStage.setWidth(500);
  primaryStage.setHeight(400);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(ProgressBarApp.class, args);
 }
}




As you see the output, when the progress bar is in indeterminate state, it moves from left to right and right to left.



Previous                                                 Next                                                 Home

No comments:

Post a Comment