Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JProgressBar not painting -- too fast?

843804Jul 18 2005 — edited Sep 13 2006
I've made a class that shows a JProgressBar in a JFrame with a JTextArea underneath.

I use this class when my app first loads, and the JProgressBar shows the progress as it should.

I also use a new instance of this class when my app loads some data from the internet, but for some reason the JFrame is empty and just doesn't display its contents. I must admit that the JFrame is only visible for < 1 second, but I'd like the JProgressBar to be visible and show its progress even though it's finished very fast.

I've tried calling repaint(), validate, toFront(), requestFocus() methods, but none work. I just get the grey box.

I don't get it...it's the same code, and used in the same manner...

any ideas?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SplashScreen extends JFrame
{
   JProgressBar progressBar;
   JTextArea taskOutput;
   Container c;
   JPanel panel;
   int progress = 0;
   int numStages;

   public SplashScreen(String title, int numTotalStages, boolean inJar)
   {
       super(title);
       numStages = numTotalStages;

       if (inJar)
         setIconImage(new ImageSizedJPanel("icon.gif", false, "JAR").icon.getImage());
       else
         setIconImage(new ImageSizedJPanel("icon.gif", false, "").icon.getImage());

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       c = getContentPane();

       initBar();

       pack();

       Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
       setLocation( (int)(resolution.getWidth() - getPreferredSize().getWidth())/2 , (int)(resolution.getHeight() - getPreferredSize().getHeight())/2);

       show();
   }

   void initBar()
   {
       int w = 300;
       int h = 150;
       int lineH = numStages * 19;

       progressBar = new JProgressBar(0, numStages);
       progressBar.setValue(0);
       progressBar.setStringPainted(true);
       progressBar.setPreferredSize(new Dimension(w, 20));

       taskOutput = new JTextArea(5, 20);
       taskOutput.setMargin(new Insets(5,5,5,5));
       taskOutput.setEditable(false);
       taskOutput.setCursor(null); 



       JScrollPane scroller = new JScrollPane(taskOutput);
       scroller.setPreferredSize(new Dimension(w, lineH));

       panel = new JPanel(new FlowLayout());
       panel.setPreferredSize(new Dimension(w + 15, h + 35));
       panel.add(progressBar);
       panel.add(scroller);

       c.add(panel);
   }

   void updateProgress(String update)
   {
     if (progress > 0)
       taskOutput.append("...done \n" + update);
     else
       taskOutput.append(update);

     progress++;
     progressBar.setValue(progress);

     c.repaint();
     // this is where i have tried to refresh

     if (progress >= numStages)
       dispose();
   }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 11 2006
Added on Jul 18 2005
8 comments
188 views