Thursday, February 16, 2012

JavaFX 2.0: ProgressBar and ProgressIndicator

Example of ProgressBar in JavaFX 2.0. You can try clicking on the "+" and "-" buttons to change the progress to greater than 100% and lower than 0%, to view how it show on ProgressBar and ProgressIndicator.
JavaFX 2.0: ProgressBar and ProgressIndicator
package javafx_progressbar;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_ProgressBar extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("http://java-buddy.blogspot.com/");

VBox root = new VBox();
HBox hBox1 = new HBox();
HBox hBox2 = new HBox();
HBox hBox3 = new HBox();
hBox1.setSpacing(3);
hBox2.setSpacing(3);
hBox3.setSpacing(3);

//Without set progress
ProgressBar myProgressBar1 = new ProgressBar();
ProgressIndicator myProgressIndicator1 = new ProgressIndicator();
myProgressIndicator1.setProgress(myProgressBar1.getProgress());
hBox1.getChildren().add(myProgressBar1);
hBox1.getChildren().add(myProgressIndicator1);

//With fixed progress
ProgressBar myProgressBar2 = new ProgressBar();
myProgressBar2.setProgress(0.3);
ProgressIndicator myProgressIndicator2 = new ProgressIndicator();
myProgressIndicator2.setProgress(myProgressBar2.getProgress());
hBox2.getChildren().add(myProgressBar2);
hBox2.getChildren().add(myProgressIndicator2);

//+/- progress
final ProgressBar myProgressBar3 = new ProgressBar();
myProgressBar3.setProgress(0.1);
final ProgressIndicator myProgressIndicator3 = new ProgressIndicator();
myProgressIndicator3.setProgress(myProgressBar3.getProgress());

Button buttonInc = new Button("+");
Button buttonDec = new Button("-");

buttonInc.setOnAction(new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent arg0) {
myProgressBar3.setProgress(myProgressBar3.getProgress() + 0.1);
myProgressIndicator3.setProgress(myProgressBar3.getProgress());
}
});

buttonDec.setOnAction(new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent arg0) {
myProgressBar3.setProgress(myProgressBar3.getProgress() - 0.1);
myProgressIndicator3.setProgress(myProgressBar3.getProgress());
}
});

hBox3.getChildren().add(buttonDec);
hBox3.getChildren().add(myProgressBar3);
hBox3.getChildren().add(myProgressIndicator3);
hBox3.getChildren().add(buttonInc);

root.getChildren().add(hBox1);
root.getChildren().add(hBox2);
root.getChildren().add(hBox3);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}


3 comments:

  1. Good looking post. I tried to compile this in JavaFX 2.2 and it didn't like a number of lines starting with line 42: myProgressIndicator1.
    setProgress(myProgressBar1.
    getProgress());
    The error message was that getProgress() wasn't a known subroutine

    ReplyDelete
    Replies
    1. hello tchite,

      I re-tried again, and worked as expected. Are you sure "import javafx.scene.control.ProgressBar;"?

      for your reference:
      http://docs.oracle.com/javafx/2/api/javafx/scene/control/ProgressBar.html

      Delete
  2. How i can make a progress bar using indeterminate mode only in one direction javafx??

    Please reply me.

    i will be very thankful.

    ReplyDelete