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

  • SEARCH
  • Home
  • 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 8493989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:10:32+00:00 2026-06-10T23:10:32+00:00

I have a problem with the CardLayout show method So I declare my CardLayout

  • 0

I have a problem with the CardLayout show method

So I declare my CardLayout and apply it to my JPanel

CardLayout cl = new CardLayout();
panel.setLayout(cl);

Then I add a 2 panels into the CardLayout

cl.addLayoutComponent(panel, "menuScreen");
cl.addLayoutComponent(panel1, "gameScreen");

I then have a JButton that when is clicked, I show the gameScreen

public void mouseClicked(MouseEvent e) {
    if(e.getSource() ==  (startGame))

    scenechange.show(panel,"gameScreen");
}

The only problem is that it doesn’t go to my gameScreen. It gives me an llegalArgumentException.
It says “Exception in thread “AWT-EventQueue-0″ java.lang.IllegalArgumentException: wrong parent for CardLayout”.

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-10T23:10:34+00:00Added an answer on June 10, 2026 at 11:10 pm

    You need to have three panels. The parent and the two cards.

    Currently you have “panel” as the parent and one of the childen.

    CardLayout cl = new CardLayout();
    panel.setLayout(cl);
    

    and

    cl.addLayoutComponent(panel, "menuScreen");
    

    Consider this code sample from the Java trail

     ....
     //Create the "cards".
        JPanel card1 = new JPanel();
        card1.add(new JButton("Button 1"));
    
        JPanel card2 = new JPanel();
        card2.add(new JTextField("TextField", 20));
    
        //Create the panel that contains the "cards".
        cards = new JPanel(new CardLayout());
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of panels put in a CardLayout where n th panel
I have problem with show or hide form in Window Form Application. I start
I have problem while loading data into html select when users press or click
I have problem with Uploadify: I log in the project using: FormsAuthentication.SetAuthCookie(myName, false); Then,
I have written a program which uses CardLayout. I want it to show a
I have problem adding markers into marker Clusterer of Google Maps. Firebug returns error:
I have problem with List<Image> List<Image> _Images = new List<Image>(); int currIndex = 0;
I am new to Java, started learning swing and have a problem with resizing
I have a Main JPanel, and its layout is set to the CardLayout. Main
Have problem with this code var MAIN_LOCATION = http://www.bosscaffe.com/new/; $(#gallery_page).click(function() { $('#gallery_photos').show(); getPhotos(); return

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.