Search code examples
javaswingjframejinternalframejdesktoppane

Issue Getting a JInternalFrame to Appear


So, I have, at this point, just a simple JFrame with a simple JPanel inside of it. (That is, it's contentPane is a JPanel.) There's a menu bar in the JFrame with a button that converts the current JFrame to a JInternalFrame - it set's the JFrame's contentPane to a pre-existing JDesktopPane and moves the JPanel that was inside the JFrame to the newly created JInternalFrame - and creates a second JInternalFrame with a new JPanel inside it. Here's my code:

if(ae.getActionCommand().equals("newWindow"))
{
    if(jdp.getComponentCount() > 0)//jdp is the pre-existing JDesktopPane
    {
        //All of the code in this if statement works fine. It's the else in which I am getting problems.
        DefaultInternalFrame dif = new DefaultInternalFrame();//DefaultInternalFrame is an extension of JInternalFrame which is literally nothing more than a JInternalFrame right now.
        jdp.add(dif);
        dif.setContentPane(new DefaultPanel());//Much like DefaultInternalFrame, DefaultPanel is just an extension of JPanel which I plan on adding to, but have not yet.
        dif.setVisible(true);
        dif.moveToFront();
    }else
    {
        //Again, this is where I'm having issues..
        DefaultPanel dp = (DefaultPanel)baseFrame.getContentPane();
        jdp.setVisible(true);
        baseFrame.setContentPane(jdp);
        DefaultInternalFrame dif = new DefaultInternalFrame();
        jdp.add(dif);
        dif.setContentPane(dp);
        dif.setVisible(true);
        dif.moveToFront();

        DefaultInternalFrame dif2 = new DefaultInternalFrame();
        jdp.add(dif2);
        dif2.setContentPane(new DefaultPanel());
        dif2.setVisible(true);
        dif2.moveToFront();
    }
    arrangeHorizontally();//This takes care of resizing and relocating the JInternalFrames. (It is definitely not the problem.)
}

The problem I'm having is that it seems that the JDesktopPane is getting higher priority. That is, after this code has executed, I don't see two JInternalFrames, I see the JDesktopPane. And it's not because of issues with size or location of the JInternalFrame. I have checked that extensively (by printing the size and location after the arrangeHorizontally() method.) So, I'm at a loss. Any help?

SSCCE:

private static JFrame f;
private static JDesktopPane desktop;
public static void main(String[] args) throws InterruptedException
{
    desktop = new JDesktopPane();

    f = new JFrame("Test");
    f.setPreferredSize(new Dimension(500,500));
    f.setContentPane(new JPanel());

    f.pack();
    f.setVisible(true);

    Thread.sleep(4000); //Just so you can see the before/after

    JPanel panel = (JPanel)f.getContentPane();

    desktop.setVisible(true);
    f.setContentPane(desktop);

    JInternalFrame inFrame = new JInternalFrame("1");
    desktop.add(inFrame);
    inFrame.setContentPane(panel);
    inFrame.setPreferredSize(new Dimension(200,200));//Just some random size; doesn't matter.
    inFrame.pack();
    inFrame.setVisible(true);

    JInternalFrame inFrame2 = new JInternalFrame("2");
    desktop.add(inFrame2);
    inFrame2.setContentPane(new JPanel());
    inFrame2.setPerferedSize(new Dimension(200,200));
    inFrame2.pack();
    inFrame2.setVisible(true);
}

That should work.. Okay, the SSCCE actually works as it should.. Which makes me wonder, why isn't the original working?


Solution

  • The change in content pane might not be enough for the frame to update itself (you think it would be, but hay).

    In your switch code, try adding a call to validate from the frame...

    //Again, this is where I'm having issues..
    DefaultPanel dp = (DefaultPanel)baseFrame.getContentPane();
    jdp.setVisible(true);
    baseFrame.setContentPane(jdp);
    DefaultInternalFrame dif = new DefaultInternalFrame();
    jdp.add(dif);
    dif.setContentPane(dp);
    dif.setVisible(true);
    dif.moveToFront();
    
    DefaultInternalFrame dif2 = new DefaultInternalFrame();
    jdp.add(dif2);
    dif2.setContentPane(new DefaultPanel());
    dif2.setVisible(true);
    dif2.moveToFront();
    
    baseFrame.validate(); // <-- Call me
    

    You might also need a call to repaint, but see where this gets you.