JavaFX Change text under Progress Indicator (default: Done)

房东的猫 提交于 2020-01-05 08:02:09

问题


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



来源:https://stackoverflow.com/questions/15972387/javafx-change-text-under-progress-indicator-default-done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!