使用JavaFX UI控件的ProgressIndicator时,如何避免显示百分比值 [英] How can I avoid the display of percentage values, when using the ProgressIndicator of JavaFX UI Controls
问题描述
我对Java很陌生,想要摆脱Java FX中显示的ProgressIndicator百分比值。是否有禁用值显示的方法?我检查了文档,但是据我所知,无法找到正确的方法。
感谢您的帮助!!
I am pretty new to Java and would like get rid of the displayed percentage value of the ProgressIndicator in Java FX. Is there a method to disable the value display? I checked the documentation, but as far as I understand it and couldn't find the right method. Thank you for your help!!
推荐答案
编辑文本值
由于用于显示进度值的 Text
节点隐藏在 ProgressIndicatorSkin中的内部类中
类,访问它的最佳方法是使用查找,尝试通过其样式百分比
查找此节点。
Since the Text
node used for displaying the progress value is hidden in an inner class within the ProgressIndicatorSkin
class, the best way to access it is using lookups, trying to find this node by its styleclass percentage
.
此代码段只会删除%
字符。
This snippet will just remove the %
character.
private Text lookup;
@Override
public void start(Stage primaryStage) {
final Group root = new Group();
final ProgressIndicator indicator = new ProgressIndicator();
root.getChildren().add( indicator );
final Scene scene = new Scene( root );
final Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
IntStream.range(0, 1000).forEach( i ->{
updateProgress( i, 1000 );
try{ Thread.sleep(10); } catch(InterruptedException ie){}
});
return null;
}
};
indicator.progressProperty().bind( task.progressProperty() );
new Thread( task ).start();
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
indicator.progressProperty().addListener((obs,n,n1)->{
if(lookup==null){
lookup= (Text)root.lookup(".percentage");
}
lookup.setText(lookup.getText().replace("%", ""));
});
}
删除文字值
完全不同的问题是摆脱 Text
节点。
A complete different problem is getting rid of the Text
node.
有一个名为 doneText
的静态 Text
仅用于了解已完成的大小,因为这是我们需要允许的最大文本
is just used to know the size of done as that is the biggest text we need to allow for
因此对 lookup
节点的任何更改都不会影响整个控件的边界框。
So any change to the lookup
node won't affect the bounding box of the whole control.
此外,鉴于 Text
节点是 Region
的子节点,子项列表不可修改。
What's more, given that the Text
node is child of a Region
, the children list is not modificable.
因此,我提出的解决方法就是剪切指标。
So the workaround I've come up with is just clipping the indicator.
private Text lookup;
@Override
public void start(Stage primaryStage) {
final VBox root = new VBox();
final ProgressIndicator indicator = new ProgressIndicator();
root.getChildren().addAll( new Group(indicator), new Button("Some Button") );
final Scene scene = new Scene( root );
final Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
IntStream.range(0, 1_000).forEach( i ->{
updateProgress( i, 1_000 );
try{ Thread.sleep(10); } catch(InterruptedException ie){}
});
return null;
}
};
indicator.widthProperty().addListener((obs,d,d1)->{
if(d.doubleValue()>0){
// Clip the indicator
Rectangle clip=new Rectangle(d1.doubleValue(),d1.doubleValue());
indicator.setClip(clip);
}
});
indicator.progressProperty().bind( task.progressProperty() );
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
new Thread( task ).start();
}
现在我们可以正确布局其他控件。
Now we can layout other controls properly.
这篇关于使用JavaFX UI控件的ProgressIndicator时,如何避免显示百分比值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!