Create a virtual desktop in your application : MDI « Swing JFC « Java

Create a virtual desktop in your application


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class Main extends JFrame {
  public Main() {
    JMenuBar bar = new JMenuBar();
    JMenu addMenu = new JMenu("Add");
    JMenuItem newFrame = new JMenuItem("Internal Frame");

    final JDesktopPane theDesktop = new JDesktopPane();

    newFrame.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JInternalFrame frame = new JInternalFrame("Internal Frame", true, true, true, true);

        Container c = frame.getContentPane();
        MyJPanel panel = new MyJPanel();

        c.add(panel, BorderLayout.CENTER);

    setSize(500, 400);

  public static void main(String args[]) {
    Main app = new Main();

class MyJPanel extends JPanel {
  public MyJPanel() {
    add(new JLabel("adf"));



Related examples in the same category

1.Dynamic menu item for MDI children window and scroll barDynamic menu item for MDI children window and scroll bar
2.Getting All Frames in a JDesktopPane Container
3.Creating a JDesktopPane Container
4.Build multiple document interface by yourselfBuild multiple document interface by yourself