Cameron Lattz
Cameron Lattz

Reputation: 83

JPanel doesn't display in JFrame

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.

Upvotes: 0

Views: 1027

Answers (1)

Bruno Flávio
Bruno Flávio

Reputation: 778

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)

Upvotes: 1

Related Questions