0

I am having some difficulty getting a JDesktopPane (that contains a JInternalFrame) to add to a JPanel. What is the proper way to do this? What am I doing wrong?

Here is my bare bones example:

import javax.swing.*;
import java.awt.*;

public class MainPanel extends JPanel {

    JDesktopPane jDesktopPane = new JDesktopPane();
    JInternalFrame jInternalFrame = new JInternalFrame();

    public MainPanel() {

        jDesktopPane.add(jInternalFrame);
        add(jDesktopPane);
        setSize(400,400);
        setVisible(true);
    }

    private static void createAndShowGui() {

        JFrame frame = new JFrame("This isn't working...");
        MainPanel mainPanel = new MainPanel();
        frame.setLayout(new BorderLayout());

        frame.add(mainPanel, BorderLayout.CENTER);
        frame.setContentPane(mainPanel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationByPlatform(false);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
feltersnach
  • 406
  • 3
  • 20
  • Why don't you read the Swing tutorial??? I have given you links to the Swing tutorial in the past for a reason!!! It is always a good place to start for working examples. Why are you creating a JPanel first? Why don't you just add the desktop pane to the frame? – camickr Sep 12 '15 at 01:29
  • @camickr I need to add it to a panel so I can add the panel to a GUI that I am creating with a changeListener. – feltersnach Sep 12 '15 at 01:35
  • Again why do you think you need to add it to a panel first? A desktop pane is a component just like a JPanel. It can be added anywhere a panel can be added. In any case, why did you not read the Swing tutorial??? It shows you how to add an ingernal frame to a desktop. Why did your example not look like the tutorial code? Why do you expect someone to fill in the details when you have working code to look at from the tutorial. If there is something from the tutorial you don't understand then ask a specific question. By the way the content pane of a frame is a panel. – camickr Sep 12 '15 at 01:50

1 Answers1

3
  • JDesktop doesn't use a layout manager, so it's default/preferred size is 0x0
  • JPanel uses FlowLayout by default, which honours the preferredSize of it's child components when it lays them out

So, in your constructor, you could try changing the default layout manager to BorderLayout instead...

public MainPanel() {
    setLayout(new BorderLayout());
    jDesktopPane.add(jInternalFrame);
    add(jDesktopPane); 
    // pointless
    //setSize(400,400);
    // pointless
    //setVisible(true);
}

Now, you because nothing is actually defining a preferred size for anything, you should provide your own...

public Dimension getPreferredSize() {
    return new Dimension(400, 400);
}

Then when you create the UI you can simply pack the frame...

private static void createAndShowGui() {

    JFrame frame = new JFrame("This should be working now...");
    MainPanel mainPanel = new MainPanel();
    frame.setLayout(new BorderLayout());

    // pointless considering the setContentPane call
    //frame.add(mainPanel, BorderLayout.CENTER);
    frame.setContentPane(mainPanel);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.pack();
    frame.setLocationByPlatform(false);
    //frame.setSize(500, 500);
    frame.setVisible(true);
}

Now because JDesktopPane doesn't use any layout manager, you become responsible for ensuring that anything your add to it is positioned and size

jInternalFrame.setBounds(10, 10, 200, 200);
// Just like any frame, it's not visible when it's first created
jInternalFrame.setVisible(true); 
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366