Friday 31 August 2012

progressbar


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class pbr extends JFrame    implements ActionListener
{
    Container c;
    Timer tm;
    JButton b1,b2,b3;
    JTextField tf;
    JProgressBar pbr;
    JLabel l;
    int i=0;

    pbr()
    {
        super("Progress Bar Demo");
        setLayout(null);
        setSize(500,200);

        c = getContentPane();
        ImageIcon imageicon = new ImageIcon("cut.gif");
        pbr = new JProgressBar();
        pbr.setMinimum(0);
        pbr.setMaximum(100);
        pbr.setStringPainted(true);
        pbr.setValue(i);
        pbr.setVisible(true);
        b1 = new JButton("Start");        b2 = new JButton("Stop");       
        b3 = new      JButton("Exit");
        l=new JLabel("Loading....");
        b1.setMnemonic('s');
        b1.setToolTipText("Click here to Start Loading");
        tf = new JTextField();
        c.add(b1).setBounds(10, 100, 70, 20);      
      
        c.add(b2).setBounds(100, 100, 70, 20);
        c.add(b3).setBounds(180, 100, 70, 20);
        c.add(l).setBounds(10,50,80,20);
        c.add(pbr).setBounds(150, 50, 200, 20);

        b1.addActionListener(this);         
        b2.addActionListener(this);
        b3.addActionListener(this);
         setVisible(true);
       tm=new Timer(100,  new ActionListener()
        {
         public void actionPerformed(ActionEvent evt)
          {
                    pbr.setValue(i);
                     i+=10;    
           }
                          });
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent windowevent)
{
                                 System.exit(0); 
 }
        });
}
public void actionPerformed(ActionEvent ae)
    {
           
            if(ae.getSource() == b1)
                    {
                        tm.start();
                              b1.setEnabled(false);
                              b2.setEnabled(true);
                     }
            if(ae.getSource() == b2)
                    {
                              b1.setEnabled(true);
                              b2.setEnabled(false);
                        tm.stop();
                     }
            if(ae.getSource() == b3)
                    {
                        System.exit(0);
                     }
    }
    public static void main(String args[])
    {
        pbr pb1 = new pbr();
    }

}


No comments:

Post a Comment