How can I avoid the display of percentage values, when using the ProgressIndicator of JavaFX UI Controls

╄→尐↘猪︶ㄣ 提交于 2019-12-02 21:29:18

问题


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!!


回答1:


Editing the Text value

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("%", ""));
    });         
}

Removing the Text Value

A complete different problem is getting rid of the Text node.

There's a static Text called doneText which

is just used to know the size of done as that is the biggest text we need to allow for

So any change to the lookup node won't affect the bounding box of the whole control.

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.



来源:https://stackoverflow.com/questions/28877859/how-can-i-avoid-the-display-of-percentage-values-when-using-the-progressindicat

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