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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:41:13+00:00 2026-06-16T02:41:13+00:00

I am trying to add and remove panel on a swing window JFrame Container

  • 0

I am trying to add and remove panel on a swing window JFrame Container with the help of following code. JPanel is being added if it added in the constructor but its not being added runtime .

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class test extends JFrame implements ActionListener {
    test() {
        Container cp = this.getContentPane();
        JButton b1 = new JButton("add");
        JButton b2 = new JButton("remove");
        cp.add(b1);
        cp.add(b2);
        b1.addActionListener(this);
        b2.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getActionCommand().equals("add")) {
            panel1 frm = new panel1();
            cp.add(frm);
        }
        if (ae.getActionCommand().equals("remove")) {
            remove(frm);
        }
    }

    public static void main(String args[]) {
        test t1 = new test();
        t1.show(true);
    }
}

class panel1 extends JPanel {
    panel1() {
        JButton b1 = new JButton("ok");
        add(b1);
    }
}
  • 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-16T02:41:15+00:00Added an answer on June 16, 2026 at 2:41 am
    1. for your concept (after remove or add JPanel to JFrame) have to call validate() & repaint() to JFrame

    2. better could be to use CardLayout

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

Sidebar

Related Questions

I am trying to add and remove table rows with jQuery, added rows contain
I am trying to add/remove UILabels to a ScrollView. The adding takes place just
I am trying to setup jQuery rows, with add/remove row functionality. I got started
I'm trying to create a player who can add and remove items from their
I'm trying to remove a class attribute from an element and add the class
I am trying add picture boxes dynamically. My code is as PictureBox picture =
I'm trying to add to/remove from a nested observableArray in KnockoutJS. I have an
I am trying to add and remove elements from a list mapped as .HasMany(),
I am trying to add and remove things in a string with using arrays.
I am trying to dynamically add list(with its items) to my container. Instead of

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.