0

I'm making a JFrame that will load pictures from the Internet. I have that working, but the problem with this JFrame is that there are many pictures, and so they take quite a while to load. This is fine, but I would like to show the user that the pictures are loading. For some reason, I can't get the JPanel to display in the loading JFrame. I know this is a common error, and I've tried many fixes, but none of them work. Here is the code:

final JFrame weatherLoadPop=new JFrame("Loading weather...");
weatherLoadPop.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
weatherLoadPop.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
      weatherPop.dispose();
   };
});
weatherLoadPop.setResizable(false);
weatherLoadPop.setBounds(100,50,225,100);
JPanel weatherLoadPanel=new JPanel();
weatherLoadPanel.setBackground(Color.RED);

weatherLoadPanel.setPreferredSize(new Dimension(225,100));
JLabel weatherLoadLabel=new JLabel("Loading...0%");
weatherLoadPanel.add(weatherLoadLabel);
weatherLoadPop.add(weatherLoadPanel);
weatherLoadPop.pack();
weatherLoadPop.validate();
weatherLoadPop.setVisible(true);

I'm not sure I'm using pack() and validate() correctly. I don't use them often. In any case, removing them does not help. The strangest part of this problem, to me, is that the JFrame that loads the pictures works beautifully, while the much simpler loading JFrame doesn't.

Thanks for any help.

1 Answers1

1

It's working fine here. Maybe you should provide an sscce that we can test?

I had to change your event listener to dispose weatherLoadPop instead of weatherPop and added your code into a test class:

package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        final JFrame weatherLoadPop = new JFrame("Loading weather...");
        weatherLoadPop.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        weatherLoadPop.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                weatherLoadPop.dispose();
            }
        ;
        });
        weatherLoadPop.setResizable(false);
        weatherLoadPop.setBounds(100, 50, 225, 100);
        JPanel weatherLoadPanel = new JPanel();
        weatherLoadPanel.setBackground(Color.RED);

        weatherLoadPanel.setPreferredSize(new Dimension(225, 100));
        JLabel weatherLoadLabel = new JLabel("Loading...0%");
        weatherLoadPanel.add(weatherLoadLabel);
        weatherLoadPop.add(weatherLoadPanel);
        weatherLoadPop.pack();
        weatherLoadPop.validate();
        weatherLoadPop.setVisible(true);
    }
}

and I'm getting:

Loading....

using:

java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)
Bruno Flávio
  • 778
  • 10
  • 27
  • Thanks Qsp. Not quite sure what an ssce is. I clicked the link and read the page, but isn't the code you provided an ssce? I would release more of the code, but my program is closed source. After commenting out the rest of the function, it worked. In fact it was ALWAYS working, but the JPanel did not display until the function which contains it finished. I never saw the JPanel display because I called dispose() on it before the function ended. Why is it waiting until the function completes before displaying the JPanel, and how do I fix that? Thank you very much for the help, Cameron – Cameron Lattz Jul 07 '12 at 01:50
  • An SSCCE is exactly what i've posted, and something you should do yourself, so that people will help you faster (they won't have to prepare a class and can copy-past and test/fix your code). – Bruno Flávio Jul 09 '12 at 11:48
  • Take a look at this [great answer](http://stackoverflow.com/a/9976255/856202) on a similar problem. If I understand you correctly your swing thread is freezing while the operation on your function is executing. So you should launch your function inside a new thread, thus preventing the lock on the display of the JPanel. If you need further help please say. – Bruno Flávio Jul 09 '12 at 11:50