by Dinesh Thakur

Progress bar is a user friendly control which is used to indicate the current status of some time consuming tasks such as software installation etc. It indicates the progress of a task by showing the percentage of completion using a color bar. The progress bar can be of two types namely, horizontal progress bar and vertical progress bar. In horizontal progress bar, the progress is indicated by filling the bar from left to right and in vertical progress bar, the progress is indicated by filling the bar from top to bottom. ยท

A progress bar is an object of class JProgressBar which is a subclass of JComponent.

JProgressBar also implements the interface SwingConstants.

Some of the constructors defined by the JProgressBar class are as follows.

JProgressBar()

JProgressBar(int orientation)

JProgressBar(int min, int max)

JProgressBar(int orientation, int min, int max)

where,

orientation defines the orientation of the progress bar as VERTICAL or HORIZONTAL

min and max define the minimum and maximum value for the progress bar, respectively. The default values are 0 and 100

 

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class JavaExampleProgressBarEventInJApplet extends JApplet
 {
    JProgressBar PrgsBar = new JProgressBar();
    JButton BtnIncrmnt = new JButton("Increase The Progress Bar");
    public void init()
      {
        Container Cntnr = getContentPane();
        Cntnr.setLayout(new FlowLayout());
        Cntnr.add(BtnIncrmnt);
        PrgsBar.setForeground(Color.blue);
        Cntnr.add(PrgsBar);
        BtnIncrmnt.addActionListener(new ActionListener()
          {
               public void actionPerformed(ActionEvent e1)
                 {
                    PrgsBar.setValue(PrgsBar.getValue()+15);
                 }
          });
                    PrgsBar.addChangeListener(new ChangeListener()
                    {
                           public void stateChanged(ChangeEvent e2)
                             {
                                showStatus("Progress Bar Mininum:"+PrgsBar.getMinimum()+"Maximum :"+PrgsBar.getMaximum() +" value: " +PrgsBar.getValue());
                             }
                        });
        }
 }
/*<APPLET CODE =JavaExampleProgressBarEventInJApplet.class WIDTH=400 HEIGHT=200></APPLET>*/

JProgressBar Event in Java Example