How to add text on jprogressbar?
Asked Answered
S

8

22

I am using a jprogressbar to indicate the availability status. i want to display a text of 40%[assumption] inside the progressbar. how to do it? the text was changed according to the availability value

Squinteyed answered 2/8, 2012 at 11:30 Comment(0)
U
44

You can use:

Initialising:

progressBar.setStringPainted(true);

Updating:

progressBar.setValue(newValue);
Ureter answered 2/8, 2012 at 11:36 Comment(2)
No need for this line: progressBar.setString(newValue + "%");Rube
The setString line may be helpful to include for people who want to display something other than a percentage. It helped me :)Isosteric
R
4
  • Use setStringPainted(true) to show the Percentage of work completed.

  • Use setValue() which will help setting the incremental value and setString() to display the end message when done...

Here is an example from my code base :

final JProgressBar bar = new JProgressBar(0 , 100);  // 0 - min , 100 - max
bar.setStringPainted(true);
panel.add(bar);                   // panel is a JPanel's Obj reference variable

JButton butt = new JButton("start");
butt.addActionListener(){

    public void actionPerformed(){
        new Thread(new Runnable(){
            public void run(){
                int x = 0;
                while(x<=100) {
                    x++;
                    bar.setValue(x);        // Setting incremental values
                    if (x ==  100 ){
                        bar.setString("Its Done");   // End message
                        try{
                            Thread.sleep(200);
                        }catch(Exception ex){ }
                    }
                }).start();
            }
        });
Recital answered 2/8, 2012 at 11:55 Comment(1)
better, confortable and easiest that SwingWorkerCarbine
C
3
Carbine answered 2/8, 2012 at 11:52 Comment(0)
B
2

This will show the progress inside the bar

   progressBar.setStringPainted(true);
Bergen answered 2/8, 2012 at 11:34 Comment(0)
M
1

This shows the progress percentage inside the progress bar

progressBar.setStringPainted(true);
Mathildemathis answered 23/10, 2017 at 14:5 Comment(0)
M
0

I'm unclear if your [assumption] is part of the string you want displayed. If so, the complete solution would be something like:

private static final String PROGRESS_MASK = "%d%% [assumption]";

public void someMethod() {
  ...

  progressBar.addChangeListener(new ChangeListener() {
    @Override
    void stateChanged(ChangeEvent e) {
      progressBar.setString(String.format(PROGRESS_MASK,
        progressBar.getValue()));
    }
  }

  progressBar.setStringPainted(true);
}

... as you would be unable to rely on the default string which merely displays the percentage.

Mnemonics answered 2/8, 2012 at 11:52 Comment(0)
H
0

Two things you should notice here. Those are,

1) You have to set paintString variable of JProgressBar using setStringPainted method. You can do that like

jprogressBar.setStringPainted(true)

you have to do this because,

isStringPainted()

method should return true, if the progress bar has to show the values or percentage of progress on it.

2) Now to customize with your custom value, set the your custom on jprogressBar instance using

jprogressBar.setString(customString)

then it should work fine.

Hypophysis answered 2/8, 2012 at 11:54 Comment(0)
E
0

Here is the tutorial link which shows how to set the value (i.e. 10% or 40%...) according to the status of the progress bar http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html

Exon answered 2/8, 2012 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.