Hello,
Attempting to create a progress bar for awt-based application. I'm trying use a thread to do some simple animation to give the appearance that a frame (the progress bar) is incrementing. However, frame will not paint immediately.
Here's the code:
import java.awt.*;
/**
*
* @author user
*/
public class ProgressBar3 extends Frame implements Runnable
{
private int Count;
private int Max;
private static final int FrameBottom = 24;
private static String classArray[] = null;
/** Creates a new instance of ProgressBar3 */
public ProgressBar3()
{
}
public ProgressBar3 (String Title, int TotalItems)
{
super(Title);
Count = 0;
Max = TotalItems;
// Allowing this to be resized causes more trouble than it is worth
// and the goal is for this to load and launch quickly!
setResizable(false);
//setLayout(null);
//addNotify(); // this is needed in order to show correctly
//setLayout(new BorderLayout()); // not working
/*setSize(getInsets().left + getInsets().right + 279,
getInsets().top + getInsets().bottom + FrameBottom);
*/
setSize(200, 50);
setLocation(50, 50);
setBackground(Color.white);
}
private void start()
{
Thread thread = new Thread(this);
thread.start();
}
public void run()
{
while (true)
{
try
{
Class c = Class.forName(classArray[Count]);
updateProgress();
Thread.sleep(10);
}
catch( Exception e)
{
break;
}
}
// Or, try the following
/*for (int i = 0; i < classArray.length; ++ i)
{
try
{
Class c = Class.forName(classArray[i]);
Count = Count + 1;
repaint();
}
catch (Exception e)
{
break;
}
}
*/
//repaint();
//dialog.setVisible(true);
}
// Update the count and then update the progress indicator. If we have
// updated the progress indicator once for each item, dispose of the
// progress indicator.
public void updateProgress ()
{
++Count;
if (Count == Max)
dispose();
else
repaint();
}
// Paint the progress indicator.
public void paint (Graphics g)
{
//Dimension FrameDimension = size();
Dimension FrameDimension = getSize();
double PercentComplete = (double)Count * 100.0 /(double)Max;
int BarPixelWidth = (FrameDimension.width * Count)/ Max;
// Fill the bar the appropriate percent full.
//g.setColor (Color.red);
g.setColor (Color.blue);
g.fillRect (0, 0, BarPixelWidth, FrameDimension.height);
// Build a string showing the % completed as a numeric value.
String s = String.valueOf((int)PercentComplete) + " %";
// Set the color of the text. If we don't, it appears in the same color
// as the rectangle making the text effectively invisible.
g.setColor (Color.black);
// Calculate the width of the string in pixels. We use this to center
// the string in the progress bar window.
FontMetrics fm = g.getFontMetrics(g.getFont());
int StringPixelWidth = fm.stringWidth(s);
g.drawString(s, (FrameDimension.width - StringPixelWidth)/2, FrameBottom);
}
public static void main(String[] args)
{
ProgressBar3 progressBar = new ProgressBar3("", args.length+1);
classArray = args;
progressBar.setVisible(true);
//progressBar.run();
progressBar.start();
}
} // end class
any help is appreciated.