1

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Steven
  • 1,709
  • 3
  • 17
  • 27
  • 1
    You're going to need to provide a [SSCCE](http://sscce.org/) for any of that to make sense – MadProgrammer Jan 23 '13 at 01:26
  • @MadProgrammer I added one that should work the same. I haven't tested it yet, but am about to do so. – Steven Jan 23 '13 at 01:41
  • Oddly enough, you're example works just fine. I tweaked it slightly to be sure, but it seems to work for me. – MadProgrammer Jan 23 '13 at 01:55
  • @MadProgrammer Yeah, I noticed that, too... So, I have no idea what is going on with my original code.. I've tried moving parts around, but, regardless of what I do, I can't get it to work.. – Steven Jan 23 '13 at 02:01
  • I might suggest, in your original code, you call `validate` (and possibly `repaint`) on the frame – MadProgrammer Jan 23 '13 at 02:12
  • That worked! :D I had tried repainting it, but not validating it. Thanks! :) (I would accept that answer, but it's a comment, not an answer.. :P Think you could make an answer suggesting that so I can accept it, as not to hurt my acceptance rating? :) ) – Steven Jan 23 '13 at 02:18
  • Nice when you find a simple solution :D – MadProgrammer Jan 23 '13 at 02:21

1 Answers1

2

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.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366