Search code examples
javaswingawtlayout-managercardlayout

CardLayout using JMenu sending error messages


Hi guys I wanted to create a simple program where I can control each JPanel by using menuItems. For example if I select File->New where New is JmenuItem it will show a JPanel named newPanel. Also if I select Edit->Edit it will show JPanel named editPanel along with the objects added to it. So far this is what I have constructed:

public class CardLayoutwithMenuBar extends JFrame implements ActionListener {

    private JMenuBar menu;
    private JMenu fileMenu;
    private JMenu editMenu;
    private JMenu exitMenu;

    private JMenuItem openItem;
    private JMenuItem newItem;
    private JMenuItem editItem;
    private JMenuItem exitItem;

    private JPanel newPanel;
    private JPanel openPanel;
    private JPanel editPanel;
    private JPanel exitPanel;

    static private CardLayout cardView;
    static private JPanel cardPanel;


    public CardLayoutwithMenuBar(){
        this.setVisible(true);
        this.setTitle("Controlling Different Panel Using CardLayout");
        this.setSize(400, 150);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create Menu bar and add to Frame
        menu = new JMenuBar();
        this.setJMenuBar(menu);

        fileMenu = new JMenu("File");
        editMenu = new JMenu("Edit");
        exitMenu = new JMenu("Exit");

        menu.add(fileMenu);
        menu.add(editMenu);
        menu.add(exitMenu);

        newItem = new JMenuItem("New File");
        openItem = new JMenuItem("File Open");
        editItem = new JMenuItem("Edit Entry");
        exitItem = new JMenuItem("Exit");

        fileMenu.add(newItem);
        fileMenu.add(openItem);
        editMenu.add(editItem);
        exitMenu.add(exitItem);

        //Declare object cardView and cardPanel and set layout of cardPanel to CardLayout
        cardView = new CardLayout();
        cardPanel = new JPanel();
        cardPanel.setLayout(cardView);


        //Create sub-panels that would correspond to each function in the menu item ex. newItem, openItem etc...
        newPanel = new JPanel();
        openPanel = new JPanel();
        editPanel = new JPanel();
        exitPanel = new JPanel();


        //add the sub-panels to the main cardpanel 
        cardPanel.add("New", newPanel);
        cardPanel.add("Open", openPanel);
        cardPanel.add("Edit",editPanel);
        cardPanel.add("Exit",exitPanel);

        newItem.addActionListener(this);
        openItem.addActionListener(this);
        editItem.addActionListener(this);
        exitItem.addActionListener(this);

        this.getContentPane().add(cardPanel, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent evt){
        String menuItemAction = evt.getActionCommand();

            if (menuItemAction.equals("New File")){
                cardView.show(newPanel, "New");
            }
            else if (menuItemAction.equals("File Open")){
                cardView.show(openPanel, "Open");
            }
            else if (menuItemAction.equals("Edit Entry")){
                cardView.show(editPanel, "Edit");
            }
            else if (menuItemAction.equals("Exit")){
                cardView.show(exitPanel, "Exit");
            }
            else{
                System.out.println("Opppsss you pressed something else");
        }
    }   
}

When I try to run this program I always get this error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout
    at java.awt.CardLayout.checkLayout(CardLayout.java:404)
    at java.awt.CardLayout.show(CardLayout.java:526)
    at com.JMenuSample.CardLayoutwithMenuBar.actionPerformed(CardLayoutwithMenuBar.java:132)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
.... and the list goes on

Can anyone help me out to fix this? Thanks in advance!


Solution

    • have to use proper method for CardLayout - public void show(Container parent, String name)

    • then to use cardView.show(cardPanel, "New"); (insted of cardView.show(newPanel, "New");)

    • for rest of newPanel as Container to stays and to change only 2nd. parameter in String form