JavaFX更改进度指示器下的文本(默认:完成)



如何在JavaFX的ProgressIndicator下更改默认文本"Done" ?

这有点棘手,但这是可能的:

JavaFX 2.2中这样做:

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


JavaFX 8你必须首先调用applyCss()之前做查找,你不需要皮肤了:

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

将文本"Foo"更改为您完成的文本,您就可以了

我已经测试了这段代码,它应该工作得很好。: -)

最新更新