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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:35:10+00:00 2026-05-17T16:35:10+00:00

BorderLayout does something strange. If I add two panels to a Container with the

  • 0

BorderLayout does something strange. If I add two panels to a Container with the same constraint (BorderLayout.CENTER for instance), then the first one goes away, even if the second one is deleted or made invisible

It seems as though it would make sense for it to “stack” each element on top of the previous ones.

Is this correct and by design? If so, is there some documentation on it?

Has anyone else been frustrated by it? Have you a solution, such as a custom LayoutManager?

Sample code:

JFrame frame = new JFrame();
frame.setSize(500, 500);

JPanel panel1 = new JPanel();
panel1.setBackground(Color.blue);
frame.getContentPane().add(panel1);

JPanel panel2 = new JPanel();
panel2.setBackground(Color.red);
frame.getContentPane().add(panel2);

panel2.setVisible(false); // Seems like it should allow us to see panel1.

frame.setVisible(true);

This creates and displays a 500×500 blank box.

  • 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-17T16:35:11+00:00Added an answer on May 17, 2026 at 4:35 pm

    BorderLayout was simply not designed to do what you want. Separation of responsibility. If you want that behavior you should compose: combine the BorderLayout with a CardLayout. Though for the actual stack behavior, you’ll have to code something yourself (or find someone who already has.)

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

Sidebar

Related Questions

I have a JFrame with BorderLayout , there are panels on all sides (North,
I'm writing a game which uses a border layout with a JPanel using BorderLayout.CENTER.
I created a JFrame initialized with a BorderLayout and a JScrollPane as its CENTER
add and GetContentPane are methods Does this code access a method in a method?
In one window I added a JButton to a Panel within a BorderLayout. In
In the beginning of my Java code I have: import java.awt.BorderLayout; import java.awt.Container; import
first post don't hurt me :) I am using a BorderLayout with the usual
I have a JPanel inside BorderLayout.CENTER The JPanel has a Grid Layout, and I
I'm writing an app where I have a BorderLayout for the entire page. In
I have a border layout whose center panel is defined like the below. {

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.