JavaFX Change text under Progress Indicator (default: Done)


Question

how can I change default text "Done" under ProgressIndicator in JavaFX?

1
1
12/17/2014 6:58:56 PM

Accepted Answer

This is a little bit tricky but it is possible:

In JavaFX 2.2 this is made like this:

ProgressIndicator indicator = new ProgressIndicator();
ProgressIndicatorSkin indicatorSkin = new ProgressIndicatorSkin(indicator);
final Text text = (Text) indicatorSkin.lookup(".percentage");
indicator.progressProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> ov, Number t, Number newValue) {
        // If progress is 100% then show Text
        if (newValue.doubleValue() >= 1) {
            // This text replaces "Done"
            text.setText("Foo");
        }
    }
});
indicator.skinProperty().set(indicatorSkin);
indicator.setProgress(1);


In JavaFX 8 you must first call applyCss() before doing a lookup and you do not need the skin anymore:

ProgressIndicator indicator = new ProgressIndicator();
indicator.progressProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> ov, Number t, Number newValue) {
        // If progress is 100% then show Text
        if (newValue.doubleValue() >= 1) {
            // Apply CSS so you can lookup the text
            indicator.applyCss();
            Text text = (Text) indicator.lookup(".text.percentage");
            // This text replaces "Done"
            text.setText("Foo");
        }
    }
});
indicator.setProgress(1);

Change the text "Foo" to your finished Text and you are ready

I have tested this code and it should work fine. ;-)

4
12/17/2014 6:22:15 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon