Progress Monitor in Java


Here is sample Java Program thats implements a Progress Monitor.

progressTest.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class progressTest extends Object
{
public static void main(String args[])
{
JFrame frame=new JFrame(“Progress Monitor”);
JButton button=new JButton(“Start”);
frame.getContentPane().add(button,BorderLayout.CENTER);
int min=0;
int max=100;
String[] message=new String[2];
message[0]=”Performing Operation”;
message[1]=”This may take sometime…”;
final ProgressMonitor monitor=new ProgressMonitor(frame,message,”Iteration”,min,max);
final Runnable runnable=new Runnable()
{
public void run()
{
int sleepTime=500;
for(int i=1;i<100;i++)
{
try
{
monitor.setNote(“Iteration”+i);
monitor.setProgress(i);
if(monitor.isCanceled())
{
monitor.setProgress(100);
break;
}
Thread.sleep(sleepTime);
}
catch(InterruptedException dontcare)
{
}
}
monitor.close();
}
};
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Thread thread=new Thread(runnable);
thread.start();
}
}
);
frame.pack();
frame.setVisible(true);
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s