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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:34:02+00:00 2026-06-17T08:34:02+00:00

So I am writing a little app that needs to have one JPanel be

  • 0

So I am writing a little app that needs to have one JPanel be added to the same area as another and have one show up when it’s needed – AKA when a button is pressed, one disappears and the other shows up. As soon as I have time, I will clean up the post, but for now I kinda need to rush so I don’t miss the bus home.

Also, if this is not possible, please tell me a way I can replicate the effect. Preferably within the same window.

SSCCE ex. imports:

public class Demo implements ActionListener {
    static JButton switch = new JButton("Switch");

    public static void main(String[] args) {
        JFrame disp = new JFrame("Demo");
        disp.setLayout(new BorderLayout());
        disp.add(switch, BorderLayout.NORTH);
        JPanel pan1 = new JPanel();
        pan1.setBackground(Color.RED);
        disp.add(pan1);
        JPanel pan2 = new JPanel();
        pan2.setBackground(Color.GREEN);
        disp.add(pan2);
        disp.setVisible(true);
    }

    void actionPerformed(ActionEvent e) {
        System.out.println(e.paramString());
        //Something to switch the JPanels when "switch" is pressed
    }
}
  • 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-17T08:34:03+00:00Added an answer on June 17, 2026 at 8:34 am

    If I understand your question correctly, I think what you want is to use one JPanel with a CardLayout. This in turn could hold the other two colored JPanels. You can then toggle between the two. The JPanel with the CardLayout could then be added to your BorderLayout.CENTER.

    The other option is to manage this yourself. Keep references to both pan1 and pan2 as member variables. Then inside of action performed, you can simply remove pan1 and add pan2.

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

Sidebar

Related Questions

I'm writing a little app that displays some graphics and just now I'm trying
I'm writing a little Rails app & part of that is pulling in a
I have been thinking of maybe writing a little tool that logs into SO
I'm writing a little C# app that calls a few functions in a C++
I'm writing a little app for the Mac, that involves recording a voice. In
I am writing an app that the layout is a little off on the
I'm writing a Windows Phone 7 app that needs to be location aware. Specifically
I am writing a little picture frame app for android that is using opengl
I am writing a little screen-scraping app that consumes some XHTML - it goes
Just a little starter information. I am writing an Andorid app that talks 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.