[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: implementing a JProgressBar
What I did was the following:
Class name "StartupStatus.java"
---------------------------
import javax.swing.*;
import java.awt.*;
import oracle.jdeveloper.layout.*;
/**
* A Swing-based dialog class.
* <P>
* @author Kent Fletcher
*/
public class StartupStatus extends JDialog
{
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JProgressBar pb = new JProgressBar();
JLabel jLabel1 = new JLabel();
/**
* Constructs a new instance.
* @param parent
* @param title
* @param modal
*/
public StartupStatus(Frame parent, String title, boolean modal)
{
super(parent, title, modal);
try
{
jbInit();
pack();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Constructs a new non-modal unparented instance with a blank title.
*/
public StartupStatus()
{
this(null, "", false);
}
/**
* Initializes the state of this instance.
*/
private void jbInit() throws Exception
{
jPanel1.setLayout(xYLayout1);
xYLayout1.setHeight(142);
xYLayout1.setWidth(400);
jLabel1.setText("Please Wait While App is Initialized");
jLabel1.setFont(new Font("Dialog", 1, 16));
getContentPane().add(jPanel1);
jPanel1.add(pb, new XYConstraints(15, 73, 362, 22));
jPanel1.add(jLabel1, new XYConstraints(68, 40, -1, -1));
}
public void setProgressValue(int val)
{
pb.setValue(val);
}
public void setProgressMinimum(int val)
{
pb.setMinimum(val);
}
public void setProgressMaximum(int val)
{
pb.setMaximum(val);
}
}
Then to use this object, here is an example:
int MAX = whatever you want;
StartupStatus jdp = new StartupStatus(jf, "Startup Status", false);
//where jf is your parent JFrame
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = jdp.getSize();
if (frameSize.height > (screenSize.height-25))
{
frameSize.height = (screenSize.height-25);
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
jdp.setLocation((screenSize.width - frameSize.width)/2,
((screenSize.height-25) - frameSize.height)/2);
jdp.setProgressMinimum(0);
jdp.setProgressMaximum(MAX);
jdp.setVisible(true);
// put a loop here
int ctr=0;
for(int i=0; i< MAX; i++)
{
// your do your stuff here
jdp.setProgressValue(i);
}
//when your done, set visible to false.
jdp.setVisible(false);
The key is to know your MAX.
Good luck
Kent
----- Original Message -----
From: "Jason R. Kretzer" <jason@OpinionOne.com>
To: <ajug-members@ajug.org>
Sent: Monday, March 25, 2002 3:12 PM
Subject: implementing a JProgressBar
> I have an executable jar that extracts files from itself. I would like to
> have a JProgressBar show the progress of this. Below is the code I use for
> extraction. How can I implement a JProgressBar to use this? I would like
to
> increment it with each "buffered IO dump".
>
> code:
>
> JarFile thisJar = new JarFile("some.jar");
> Enumeration enum = thisJar.entries();
> enum.nextElement();
> File dir = null;
> JarEntry je=null;
> InputStream is=null;
> BufferedInputStream bi=null;
> FileOutputStream fos=null;
> BufferedOutputStream bo=null;
>
> /*fileList is an array of Strings each is a name
> a jar entry*/
> for(int i = 0;i<fileList.length;i++)
> {
> String str = fileList[i].toString();
>
> if(str.indexOf('.',0)==-1)
> {
> dir = fileList[i].getParentFile();
> dir.mkdirs();
> fileList[i].mkdirs();
> continue;
> }
>
> je = ((JarEntry)enum.nextElement());
> is = getClass().getClassLoader().getResourceAsStream(je.toString());
> bi = new BufferedInputStream(is,16384);
> fos = new FileOutputStream(fileList[i]);
> bo = new BufferedOutputStream(fos,16384);
> try
> {
> while(true)
> {
> byte [] byteArray = new byte[16384];
> int intRead = bi.read(byteArray, 0, byteArray.length);
> bo.write(byteArray, 0, intRead);
> bo.flush();
> }
> }
> catch (Exception k)
> {
> k.printStackTrace(ps);
> System.out.println("There were problems extracting the Jar!");*/
> }
> bi.close();
> bo.close();
> }
>
>
> Thanks in advance,
>
> -Jason
>
>
****************************************************************************
> ********
> Jason R. Kretzer
> Software Engineer
> Opinion One
> Email: jason@opone.com
> Phone: (513) 361-2771
> Website: http://alia.iwarp.com
>
> "Make visible what, without you, might perhaps never have been seen."
> --Robert Bresson
>
****************************************************************************
> ********
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com