Sunday, January 23, 2011

Progress Bar Demo

import java.awt.*;
import javax.swing.*;
Interface Design
class Progress extends JFrame{
PbThread t1;
JProgressBar pb;
JLabel l;

Progress() {
super("Progress Bar");
setLayout(null);
setResizable(false);
setSize(300,100);

pb=new JProgressBar();
add(pb);
pb.setBounds(10,12,270,25);
l=new JLabel();
l.setText("");
add(l);
l.setBounds(120,40,97,25);

Dimension ss=Toolkit.getDefaultToolkit().getScreenSize();
Dimension ws=getSize();
this.setBounds((ss.width-ws.width)/2,(ss.height-ws.height)/2,ws.width,ws.height);

t1=new PbThread(pb,l);
t1.start();
}
public static void main(String args[]){
Progress p=new Progress();
p.setVisible(true);
}
}

Thread to show Progress
class PbThread extends Thread{
JProgressBar pb1;
JLabel l1=new JLabel();

PbThread(JProgressBar pb,JLabel l) {
this.pb1=pb;
this.l1=l;
}
public void run(){
pb1.setMaximum(100);
pb1.setMinimum(0);
for(int x=0;x<=100;x++) {
pb1.setValue(x);
l1.setText(x+"%");
try {
sleep(100);
}
catch(InterruptedException e) {
JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);
}
if(x==100)
System.exit(0);
}
}
}

No comments:

Post a Comment