[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: implementing a JProgressBar



If you want the progress bar to get all the way to 100%, you have to figure
out some way to get a maximum reachable value for the progress bar. If you
wish, you can just choose some average number of buffered io cycle, and set
the progress bar max min to that, like so:
new JProgressBar(0,42);
-or-
m_progressBar.setMaximum(42);
In any case, do some checking to make sure you don't advance over 100%, just
to make it look professional.

Code:
// Create a progress bar
private JProgressBar m_progressBar =  new JProgressBar();    // defaults to
0 to 100 scale
m_progressBar.setStringPainted(true);
m_progressBar.setBorder(BorderFactory.createLoweredBevelBorder());
JPanel mPanel = new JPanel();
mPanel.setLayout(new java.awt.GridBagLayout());
mPanel.add(m_progressBar);
// on each buffered IO dump, advance the progress bar
m_progressBar.setValue(m_progressBar.getValue() + 1);

// to reset progress bar
m_progressBar.setValue(0);


----- 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