JavaFX Change text under Progress Indicator (defau

2019-08-14 13:40发布

问题:

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

回答1:

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. ;-)