Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Sample of overlaying a text label indicating progress on top of a JavaFX ProgressBar.
import javafx.animation.*;
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class LabeledProgressBarSample extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(final Stage stage) {
stage.setScene(new Scene(createResettableProgressIndicatorBar()));;
private VBox createResettableProgressIndicatorBar() {
final int TOTAL_WORK = 18;
final String WORK_DONE_LABEL_FORMAT = "%.0f";
final ReadOnlyDoubleWrapper workDone = new ReadOnlyDoubleWrapper();
final ProgressIndicatorBar bar = new ProgressIndicatorBar(
final Timeline countDown = new Timeline(
new KeyFrame(Duration.seconds(0), new KeyValue(workDone, TOTAL_WORK)),
new KeyFrame(Duration.seconds(10), new KeyValue(workDone, 0))
final Button resetButton = new Button("Reset");
resetButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
final VBox layout = new VBox(20);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 20px;");
layout.getChildren().addAll(bar, resetButton);
return layout;
class ProgressIndicatorBar extends StackPane {
final private ReadOnlyDoubleProperty workDone;
final private double totalWork;
final private ProgressBar bar = new ProgressBar();
final private Text text = new Text();
final private String labelFormatSpecifier;
final private static int DEFAULT_LABEL_PADDING = 5;
ProgressIndicatorBar(final ReadOnlyDoubleProperty workDone, final double totalWork, final String labelFormatSpecifier) {
this.workDone = workDone;
this.totalWork = totalWork;
this.labelFormatSpecifier = labelFormatSpecifier;
workDone.addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
bar.setMaxWidth(Double.MAX_VALUE); // allows the progress bar to expand to fill available horizontal space.
getChildren().setAll(bar, text);
// synchronizes the progress indicated with the work done.
private void syncProgress() {
if (workDone == null || totalWork == 0) {
} else {
text.setText(String.format(labelFormatSpecifier, Math.ceil(workDone.get())));
bar.setProgress(workDone.get() / totalWork);
bar.setMinHeight(text.getBoundsInLocal().getHeight() + DEFAULT_LABEL_PADDING * 2);
bar.setMinWidth (text.getBoundsInLocal().getWidth() + DEFAULT_LABEL_PADDING * 2);

This comment has been minimized.

Copy link
Owner Author

@jewelsea jewelsea commented Dec 27, 2012

Sample answer for the StackOverflow question: Draw a String onto a ProgressBar, like JProgressBar?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment