JavaFX ProgressBar and ProgressIndicator Tutorial with Examples
1. ProgressBar and ProgressIndicator
No ADS
ProgressBar and ProgressIndicator are UI controls that visualize progress of any operations in your JavaFX applications.
ProgressBar and ProgressIndicator with workload to be determined:
2. ProgressBar & ProgressIndicator Example
The following example creates a ProgressBar and a ProgressIndicator simulate a process not determine the time of the end.
ProgressDemo.java
package org.o7planning.javafx.progress;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class ProgressDemo extends Application {
@Override
public void start(Stage stage) {
ProgressBar progressBar = new ProgressBar();
ProgressIndicator progressIndicator = new ProgressIndicator();
FlowPane root = new FlowPane();
root.setPadding(new Insets(10));
root.setHgap(10);
root.getChildren().addAll(progressBar, progressIndicator);
Scene scene = new Scene(root, 400, 300);
stage.setTitle("JavaFX ProgressBar & ProgressIndicator (o7planning.org)");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
3. Progress and Task
No ADS
You can create a Task object to perform certain task, such as copying files list. Copying requires take some time, you need to use ProgressBar or ProgressIndicator to display the percentage of work already done.
ProgressAndTaskDemo.java
package org.o7planning.javafx.progress;
import java.io.File;
import java.util.List;
import javafx.application.Application;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ProgressAndTaskDemo extends Application {
private CopyTask copyTask;
@Override
public void start(Stage primaryStage) {
final Label label = new Label("Copy files:");
final ProgressBar progressBar = new ProgressBar(0);
final ProgressIndicator progressIndicator = new ProgressIndicator(0);
final Button startButton = new Button("Start");
final Button cancelButton = new Button("Cancel");
final Label statusLabel = new Label();
statusLabel.setMinWidth(250);
statusLabel.setTextFill(Color.BLUE);
// Start Button.
startButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
startButton.setDisable(true);
progressBar.setProgress(0);
progressIndicator.setProgress(0);
cancelButton.setDisable(false);
// Create a Task.
copyTask = new CopyTask();
// Unbind progress property
progressBar.progressProperty().unbind();
// Bind progress property
progressBar.progressProperty().bind(copyTask.progressProperty());
// Hủy bỏ kết nối thuộc tính progress
progressIndicator.progressProperty().unbind();
// Bind progress property.
progressIndicator.progressProperty().bind(copyTask.progressProperty());
// Unbind text property for Label.
statusLabel.textProperty().unbind();
// Bind the text property of Label
// with message property of Task
statusLabel.textProperty().bind(copyTask.messageProperty());
// When completed tasks
copyTask.addEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, //
new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
List<File> copied = copyTask.getValue();
statusLabel.textProperty().unbind();
statusLabel.setText("Copied: " + copied.size());
}
});
// Start the Task.
new Thread(copyTask).start();
}
});
// Cancel
cancelButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
startButton.setDisable(false);
cancelButton.setDisable(true);
copyTask.cancel(true);
progressBar.progressProperty().unbind();
progressIndicator.progressProperty().unbind();
statusLabel.textProperty().unbind();
//
progressBar.setProgress(0);
progressIndicator.setProgress(0);
}
});
FlowPane root = new FlowPane();
root.setPadding(new Insets(10));
root.setHgap(10);
root.getChildren().addAll(label, progressBar, progressIndicator, //
statusLabel, startButton, cancelButton);
Scene scene = new Scene(root, 500, 120, Color.WHITE);
primaryStage.setTitle("ProgressBar & ProgressIndicator");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
CopyTask.java
package org.o7planning.javafx.progress;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javafx.concurrent.Task;
// Copy all file in C:/Windows
public class CopyTask extends Task<List<File>> {
@Override
protected List<File> call() throws Exception {
File dir = new File("C:/Windows");
File[] files = dir.listFiles();
int count = files.length;
List<File> copied = new ArrayList<File>();
int i = 0;
for (File file : files) {
if (file.isFile()) {
this.copy(file);
copied.add(file);
}
i++;
this.updateProgress(i, count);
}
return copied;
}
private void copy(File file) throws Exception {
this.updateMessage("Copying: " + file.getAbsolutePath());
Thread.sleep(500);
}
}
No ADS
JavaFX Tutorials
- JavaFX TableView Tutorial with Examples
- JavaFX GridPane Layout Tutorial with Examples
- JavaFX Image and ImageView Tutorial with Examples
- JavaFX RadioButton Tutorial with Examples
- Install e(fx)clipse for Eclipse (JavaFX Tooling)
- JavaFX FileChooser and DirectoryChooser Tutorial with Examples
- JavaFX StackPane Layout Tutorial with Examples
- JavaFX ToggleButton Tutorial with Examples
- JavaFX ColorPicker Tutorial with Examples
- JavaFX ContextMenu Tutorial with Examples
- JavaFX TreeView Tutorial with Examples
- JavaFX Hyperlink Tutorial with Examples
- JavaFX DatePicker Tutorial with Examples
- JavaFX Label Tutorial with Examples
- JavaFX Accordion Tutorial with Examples
- JavaFX TilePane Layout Tutorial with Examples
- JavaFX Menu Tutorial with Examples
- JavaFX WebView and WebEngine Tutorial with Examples
- JavaFX ChoiceBox Tutorial with Examples
- JavaFX Effects Tutorial with Examples
- JavaFX TextArea Tutorial with Examples
- JavaFX MenuButton and SplitMenuButton Tutorial with Examples
- JavaFX PieChart Tutorial with Examples
- Install JavaFX Scene Builder for Eclipse
- JavaFX AreaChart and StackedAreaChart Tutorial with Examples
- JavaFX ComboBox Tutorial with Examples
- JavaFX TextInputDialog Tutorial with Examples
- JavaFX Rectangle and Ellipse Tutorial with Examples
- JavaFX HTMLEditor Tutorial with Examples
- JavaFX Button Tutorial with Examples
- JavaFX BarChart and StackedBarChart Tutorial with Examples
- Open a new Window in JavaFX
- JavaFX TitledPane Tutorial with Examples
- JavaFX AnchorPane Layout Tutorial with Examples
- JavaFX Spinner Tutorial with Examples
- JavaFX Tutorial for Beginners - Hello JavaFX
- JavaFX TreeTableView Tutorial with Examples
- JavaFX PasswordField Tutorial with Examples
- JavaFX Slider Tutorial with Examples
- JavaFX ChoiceDialog Tutorial with Examples
- JavaFX TextField Tutorial with Examples
- JavaFX ListView Tutorial with Examples
- JavaFX Alert Dialogs Tutorial with Examples
- JavaFX Line Tutorial with Examples
- JavaFX Tooltip Tutorial with Examples
- JavaFX FlowPane Layout Tutorial with Examples
- JavaFX Transformations Tutorial with Examples
- JavaFX BorderPane Layout Tutorial with Examples
- JavaFX HBox, VBox Layout Tutorial with Examples
- JavaFX Group Tutorial with Examples
- JavaFX ProgressBar and ProgressIndicator Tutorial with Examples
- JavaFX ScrollPane Tutorial with Examples
Show More