Is it possible to add JDesktopPane
and JInternalFrame
to a JPanel
if so how could I do it. I tried adding it but it couldn't work.
Asked
Active
Viewed 4,033 times
-3

Paul Samsotha
- 205,037
- 37
- 486
- 720

Praburaj
- 613
- 8
- 21
-
1As was stated in your last [question on the subject](http://stackoverflow.com/questions/22874242/adding-jdesktoppane-to-jframe-using-gridbaglayout), unless you provide an [example that demonstrates](https://stackoverflow.com/help/mcve) your problem, there's very little people to can do. The short answer is, yes. – MadProgrammer Apr 04 '14 at 23:48
-
While I agree to what MadProgrammer said, a wild guess: Did you forget to call `internalFrame.setVisible(true)` ? In any case, this should be a good starting point: http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html – Marco13 Apr 05 '14 at 00:06
1 Answers
4
"Is it possible to add JDesktopPane and JInternalFrame to a JPanel"
Sure you can
desktop = createDesktopPane();
JInternalFrame iFrame = createInternalFrame();
desktop.add(iFrame);
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(new TitledBorder("DesktopPane"));
panel.add(desktop);
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;
public class JDesktopPaneDemo1 {
private static final String URL_ONE = "http://www.hdbackgroundspoint.com/wp-content/uploads/2013/12/16/345t34.jpeg";
private static final String URL_TWO = "http://www.hdbackgroundspoint.com/wp-content/uploads/2013/09/hh.jpeg";
private Image image;
private JDesktopPane desktop;
public JDesktopPaneDemo1() {
desktop = createDesktopPane();
JInternalFrame iFrame = createInternalFrame();
desktop.add(iFrame);
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(new TitledBorder("DesktopPane"));
panel.add(desktop);
JFrame frame = new JFrame("Desktop Background");
frame.setContentPane(panel);
frame.setSize(500, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
iFrame.setVisible(true);
}
public JDesktopPane createDesktopPane() {
JDesktopPane pane = new JDesktopPane() {
private Image image;
{
try {
image = ImageIO.read(new URL(URL_ONE));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
};
return pane;
}
private JInternalFrame createInternalFrame() {
JInternalFrame frame = new JInternalFrame();
frame.setSize(200, 200);
return frame;
}
public static void main(String[] args) {
for (UIManager.LookAndFeelInfo laf : UIManager
.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
try {
UIManager.setLookAndFeel(laf.getClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException
| UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JDesktopPaneDemo1();
}
});
}
}

Paul Samsotha
- 205,037
- 37
- 486
- 720
-
-
@AndrewThompson `image = ImageIO.read(new URL(URL_ONE));` I was making a couple different examples (for a [different question](http://stackoverflow.com/a/22866831/2587435)) and I wanted to make it easier for myself to switch images – Paul Samsotha Apr 05 '14 at 02:48