Alan
Alan

Reputation: 198

Can I add JInternalFrames to a JPanel?

It seems that JInternalFrames can only be added to a JDesktopPane and you have to set your JFrame’s content pane to that JDesktopPane. Something like:

JFrame frame = new JFrame();
JDesktopPane desktopPane = new JDesktopPane();
JInternalFrame internalFrame = new JInternalFrame();

desktopPane.add(internalFrame);
frame.setContentPane(desktopPane);

The problem is that the JInternalFrames are allowed to move over anything that I add to the JFrame, like JPanels.

Is there a way for me to add the JInternalFrames/JDesktopPane to something else like a JPanel? That way I can restrict the JInternalFrames to be within that JPanel. If that is not possible, then what other options do I have?

Upvotes: 0

Views: 1359

Answers (1)

alex436
alex436

Reputation: 120

You can add any object to a JPanel that extends JComponent. For example:

JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.setVisible(true);
window.setSize(600, 400);

JDesktopPane desktopPane = new JDesktopPane();
JInternalFrame internalFrame = new JInternalFrame();        
JPanel mainPanel = new JPanel();
mainPanel.add(desktopPane);
mainPanel.add(internalFrame);
window.add(mainPanel);

hope that helps!

Upvotes: 1

Related Questions