20

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

Siddhu
  • 1,188
  • 2
  • 14
  • 24

8 Answers8

42

You can use:

Initialising:

progressBar.setStringPainted(true);

Updating:

progressBar.setValue(newValue);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
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();
            }
        });
sehe
  • 374,641
  • 47
  • 450
  • 633
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
3
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

This will show the progress inside the bar

   progressBar.setStringPainted(true);
Alexander
  • 3,019
  • 1
  • 20
  • 24
1

This shows the progress percentage inside the progress bar

progressBar.setStringPainted(true);
rghome
  • 8,529
  • 8
  • 43
  • 62
Avishka92
  • 11
  • 2
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.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
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.

sakthisundar
  • 3,278
  • 3
  • 16
  • 29
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

happy
  • 2,550
  • 17
  • 64
  • 109