by Dinesh Thakur Category: Swing

Progress indicators or progress bars are the new controls that give the users some indications of the progress of an operation. When we call JProgressBar, we can set the maximum and minimum value for the progress bar.

 

A JProgressBar is a Swing component that indicates progress. A ProgressMonitor is a dialog box that contains a progress bar. A ProgressMonitorinputStream displays a progress monitor dialog box while the stream is read. A progress bar is a simple component just a rectangle that is partially filled with color to indicate the progress of an operation. By default, progress is indicated by a string "n%".

 

You construct a progress bar much as you construct a slider, by supplying the minimum and maximum value and an optional orientation:

 

progressBar = new JProgressBar(O, 1000);

progressBar = new JProgressBar(SwingConstants.VERTICAL, 0, 1000);

 

A progress bar is a simple component that can be placed inside a window. In contrast, a ProgressMonitor is a complete dialog box that contains a progress bar (see Fig below). The dialog box contains a Cancel button. If you click it, the monitor dialog box is closed.

 

The example below shows a simple application for creating a progress bar

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
class ProgressBar extends JFrame
{
                       JProgressBar current;
                       JTextArea ta;
                        int num = 0;
                        public ProgressBar()
                {
                        Container pane=getContentPane();
                        ta=new JTextArea("");
                        pane.setLayout(new GridLayout());
                       current = new JProgressBar(0, 100);
                       current.setValue(0);
                       current.setStringPainted(true);
                       pane.add(current);
                       pane.add(ta);
                }
                          public void iterate()
                    {
                            while (num <= 100)
                                   {
                                        current.setValue(num);
                                            try
                                            {
                                                Thread.sleep(500);
                                            }
                                                 catch (InterruptedException e) { }
                                                 num += 10;
                                                 if (num>100)
                                                     {
                                                          break;
                                                      }
                                                else
                                                  ta.setText(num+"%completed");
                                     }
                      }
            public static void main(String[] args)
           {
                    ProgressBar pb = new ProgressBar();
                    pb.pack() ;
                    pb.setVisible(true);
                    pb.iterate ( );
            }
}

ProgressBar Using Swing



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content





Popular Article