Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8681117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:25:19+00:00 2026-06-12T21:25:19+00:00

Hi guys I wanted to create a simple program where I can control each

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:25:20+00:00Added an answer on June 12, 2026 at 9:25 pm
    • 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi Guys : I wanted to create a power point presentation using a raw
Hey guys I wanted to create a JScrollPane but it won't work... and I
Well... I am trying to create an application using the TTS Engine. I can
Right, hi guys, I recently followed a tutorial http://xnadevelopment.com/tutorials/thestateofthings/thestateofthings.shtml where I wanted to create
I wanted to create a macro using excel 2003 for a group of excel
Sup guys, I have a tabBarController with a navigationController on each tab. I wanted
Hy guys, I am new to android but very enthusiastic to create my first
hi guys I wanted to know, is there a way to not display my
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
Guys, I am using SQL Server 2000 and executing the sp_columns stored procedure to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.