How to make
progress bar in swing and making it stop the application when user close it?
-------------------------------------------------------------------------------------------
package
net.codejava.swing.download;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import
javax.swing.JProgressBar;
public class ProgressBarDemo extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JProgressBar
jb;
int i = 0, num = 0;
ProgressBarDemo()
{
setTitle("Launching
");
jb = new JProgressBar(0, 1000);
jb.setBounds(30, 30,
150, 40);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(250,
150);
setLayout(new FlowLayout());
//close the progress bar and stop
application processing when user click // on close button on title bar.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void progress () {
while (i <= 1000) {
jb.setValue(i);
i = i + 30;
try {
Thread.sleep(100);
}
catch (Exception e) {
}
}
}
public static void main(String[] args) {
ProgressBarDemo
m = new ProgressBarDemo();
m.setVisible(true);
// closing the frame after 100%
m.dispose();
}
}
----------------------------------------------------------------------------------------------
Output: User can close this progress window and program will be stop immediately.
----------------------------------------------------------------------------------------------
Output: User can close this progress window and program will be stop immediately.
No comments:
Post a Comment