The following code works:

public class Main {
    public static void main (String args[]){
        final JWindow f = new JWindow();
        JLabel l = new JLabel(new ImageIcon(Main.class.getResource("images/logo.png")));
        f.getContentPane().add(l, BorderLayout.CENTER);
        f.pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = l.getPreferredSize();
        f.setLocation(screenSize.width/2 - (labelSize.width/2),
        screenSize.height/2 - (labelSize.height/2));

        f.setVisible(true);
        f.setAlwaysOnTop(true);
    }
}

When I use exactly the same code in a different class (public class appForm extends javax.swing.JFrame) the screen that pops up is blank, however it does have the dimensions of the loaded image. If I try to load a different image, the dimensions of the JWindow always correspond to the dimensions of the image (so it can't be a wrong file path problem right? the image seems to be there) but it's always white.

Sounds like a rendering problem. Not sure how different the class and the other class you use are... Also, don't know how you use it in both of them???

More complete code example:
Works in Main, doesn't work in appForm

public class Main {
    public static void main (String args[]){
        final JWindow f = new JWindow();
        JLabel l = new JLabel(new ImageIcon(Main.class.getResource("images/logo.png")));
        f.getContentPane().add(l, BorderLayout.CENTER);
        f.pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = l.getPreferredSize();
        f.setLocation(screenSize.width/2 - (labelSize.width/2),
        screenSize.height/2 - (labelSize.height/2));

        f.setVisible(true);
        f.setAlwaysOnTop(true);

       appForm form = new appForm();
       form.setVisible(true);
    }
}
public class appForm extends javax.swing.JFrame {
    private void jMenuStartActionPerformed(java.awt.event.ActionEvent evt) {
        final JWindow f = new JWindow();
        JLabel l = new JLabel(new ImageIcon(Main.class.getResource("images/logo.png")));
        f.getContentPane().add(l, BorderLayout.CENTER);
        f.pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = l.getPreferredSize();
        f.setLocation(screenSize.width/2 - (labelSize.width/2),
        screenSize.height/2 - (labelSize.height/2));

        f.setVisible(true);
        f.setAlwaysOnTop(true);
    }
}

I have no idea if this is relevant, but your form.setVisible(true); seems at best redundant because the JFrame subclass to which it refers hasn't been populated or configured in any way.

Well actually appForm has a very complex gui (I'm making this in netbeans), the gui has more than a thousand lines of code, but I can't change that code, netbeans protects it, nor do I understand it actually.
That probably makes this problem pretty hopeless right?
Maybe I should look into how making a new JWindow appear should be handled in the netbeans IDE.

1) create JFrame and set its as parent for JWindow

2) why is there two Top-Level Containers JFrame and JWindow Too

3) in most cases JWindow must have to own parents tere are JFrame or JDialod, this parent must be initialized before, then you can build JWindow

4) you code is pretty out of Event Dispash Thread, you have to wrap whole main method to the invokeLater, or pack() and setVisible(true)

To answer point 2:

The entire gui uses JFrame. When the users presses a certain button I want a new screen to appear as long as the program is performing calculations. This new window must only show an animated gif/png with a spinner. I don't want that window to have a title bar and buttons to minimize and close (as is the case with a JFrame), so I opted for a JWindow.

I localized the problem:

I have the following code:

f.setVisible(true);
a lot of code for mathematical computations that takes about 20 seconds to go through
f.setVisible(false);

What happened as I stated previously was an empty window that showed up and disappeared as soon as the calculations were done. What I noticed now after removing the setVisible(false) line was that the image would eventually load in the JWindow as intended but only after the calculations were completed. Why does this happen? I have the window all set up before I make it visible, why is there a delay before the image shows up?

Your code is running on Swing's Event Dispatch Thread (EDT). Everything that Swing does (including painting/updating the screen) is run on that one thread.
So your f.setVisible(true); gets put on the EDT queue to be executed when your current method has finished - ie after the 20 sec calc.
You should use a SwingWorker to execute long-running tasks, so they don't interfere with the GUI.

ps: That explains why it works from main but not from your second window - the code in main is not executed on the EDT, so window updates can proceed while its still executing. But your second window is opened from a Swing listener method, which does execute on the EDT.

thanks for the help, swingworker is new to me, I put the calculations on a new thread and now it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.