How to add text on jprogressbar?

27,093

Solution 1

You can use:

Initialising:

progressBar.setStringPainted(true);

Updating:

progressBar.setValue(newValue);

Solution 2

  • 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();
            }
        });

Solution 3

Solution 4

This will show the progress inside the bar

   progressBar.setStringPainted(true);

Solution 5

This shows the progress percentage inside the progress bar

progressBar.setStringPainted(true);
Share:
27,093
Siddhu
Author by

Siddhu

Updated on July 09, 2022

Comments

  • Siddhu
    Siddhu almost 2 years

    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