2

Here is my code. I want to open another Java form using menu bar but when I click on menu item, it shows me an error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: 
    adding a window to a container

What should I do now?

package Driver;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

public class frmTestMenu extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frmTestMenu frame = new frmTestMenu();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public frmTestMenu() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100,653, 425);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBounds(0, 0, 434, 31);
        contentPane.add(menuBar);

        JMenu mnFile = new JMenu("FIle");
        menuBar.add(mnFile);

        JMenuItem mntmExit = new JMenuItem("Exit");
        mntmExit.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                frmTestMenu.this.dispose();
            }
        });

        mnFile.add(mntmExit);

        JMenu mnItem = new JMenu("Item1");
        menuBar.add(mnItem);

        JMenuItem mntmMenuItem = new JMenuItem("open Test Internal Form");

        mnItem.add(mntmMenuItem);

        JMenuItem mntmMenuItem_3 = new JMenuItem("open Test Internal Form 2");

        mnItem.add(mntmMenuItem_3);

        JMenu mnItem_1 = new JMenu("item 2");
        menuBar.add(mnItem_1);

        JMenuItem mntmMenuItem_1 = new JMenuItem("Menu 2 item 1");
        mnItem_1.add(mntmMenuItem_1);

        JMenu mnItem_2 = new JMenu("item 3");
        menuBar.add(mnItem_2);

        JMenuItem mntmMenuItem_2 = new JMenuItem("Menu 3 item 1");
        mnItem_2.add(mntmMenuItem_2);


        final JDesktopPane desktopPane = new JDesktopPane();
        desktopPane.setBounds(0, 0, 633, 366);
        mntmMenuItem.addMouseListener(new MouseAdapter() {
            @Override 
            public void mousePressed(MouseEvent arg0) {
                frm3_0 test1 = new frm3_0();
                test1.setBounds(10, 10,  426, 229);
                desktopPane.add(test1);
                test1.setVisible(true);
            }
        });
        mntmMenuItem_3.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                frmcity_A test2 = new frmcity_A();
                test2.setBounds(10, 10,  426, 229);
                desktopPane.add(test2);
                test2.setVisible(true);
            }
        });
        JScrollPane scrollPane = new JScrollPane(desktopPane);

        JDesktopPane desktopPane_1 = new JDesktopPane();
        desktopPane_1.setBounds(145, 88, 1, 1);
        desktopPane.add(desktopPane_1);
        scrollPane.setBounds(0, 30, 633, 336);
        contentPane.add(scrollPane);

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ahmed Abbas
  • 63
  • 1
  • 12
  • 1
    [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) from [LayoutManager](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) with JFrame.pack() after card is switched – mKorbel May 10 '15 at 09:27
  • What is the superclass of frm3_0 and frmcity_A? – Suspended May 10 '15 at 09:32
  • The name of class is frm3_0 and frmcity_A – Ahmed Abbas May 10 '15 at 09:45
  • `contentPane.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). +1 to the sage advice of @mKorbel. – Andrew Thompson May 10 '15 at 09:49
  • 1
    Use a JInternalFrame with JDeaktopPane instead of JFrame... – MadProgrammer May 10 '15 at 10:33
  • 1
    @mKorbel I didn't think you had to repack the frame after switch views with CardLayout as it uses the largest width/height properties of all the "cards" to calculate the preferred size? Could be wrong ;) – MadProgrammer May 10 '15 at 10:53
  • @MadProgrammer very good point – mKorbel May 10 '15 at 11:48

0 Answers0