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 3233998
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:20:28+00:00 2026-05-17T17:20:28+00:00

This is the problem, I have a class MainWindow that extends JFrame in one

  • 0

This is the problem, I have a class MainWindow that extends JFrame in one of the setup routines I set the layout of the class to a new CardLayout(). This all works fine but when I go to request the the layout from the JFrame and cast what it returns to a CardLayout I get errors because it is returning a BorderLayout. I think you get the picture from hear.

Please find some code below:

public MainWindow()
{
    initWindow();
}

public void actionPerformed(ActionEvent e)
{
    CardLayout m = (CardLayout)super.getLayout();
    m.next(this);
}

private void initWindow()
{
    super.getContentPane().setLayout(_mainLayout);
    super.setTitle(_WINDOW_NAME);
    super.setSize(_DEFAULT_WINDOW_SIZE);
    super.setLocationRelativeTo(null);
    super.setAlwaysOnTop(true);
    super.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    super.setResizable(false);

    init_buttons_panel();
    super.add(_buttons_panel, "bpanel");
}

I see that the code is not perfect this is because I have been trying everything to trap the problem. Yes I realize casting like that is not good but this is just test code to prove the point. Oh and _mainLayout is you guessed it a CardLayout.

I have also read how to use card layout and my code does not do anything out of the ordinary.

  • 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-05-17T17:20:29+00:00Added an answer on May 17, 2026 at 5:20 pm

    First, you should not cast a layout like that, at least without making any verifications. You should use the instanceof keyword first, to check if it really is a CardLayout, to prevent the program from finishing abruptly.

    Second, the problem is here:

    public void actionPerformed(ActionEvent e)
    {
        CardLayout m = (CardLayout)super.getContentPane().getLayout();
        m.next(this);
    }
    

    Basicly you were getting the layout of the JFrame, and not from the JFrame’s content pane, which is the one you actually set to be a CardLayout.

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

Sidebar

Related Questions

No related questions found

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.