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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:10:09+00:00 2026-06-04T11:10:09+00:00

I am trying to remove a JPanel not hide it but i can’t find

  • 0

I am trying to remove a JPanel not hide it but i can’t find anything that works.

This is the code in the panel that needs to remove itself when a button is pressed:

    play.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Frame frame = new Frame(); //referencing to my JFrame class (this class is a JPanel)
            //need to remove this panel on this line
            frame.ThreeD(); // adds a new panel
        }
    });

UPDATED

This is the full code:

package ThreeD;

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

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;

import Run.Frame;

public class Launcher extends JPanel{   
    private JButton play, options, help, mainMenu;
    private Rectangle rplay, roptions, rhelp, rmainMenu;

    private int buttonWidthLocation, buttonWidth, buttonHeight;
    private int width = 1280;

    public Launcher() {
        this.setLayout(null);

        drawButtons();
    }

    private void drawButtons() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {
            e.printStackTrace();
        }

        play = new JButton("Play");
        options = new JButton("Options");
        help = new JButton("Help");
        mainMenu = new JButton("Main Menu");

        buttonWidthLocation = (width / 2) - (buttonWidth / 2);
        buttonWidth = 80;
        buttonHeight = 40;

        rplay = new Rectangle(buttonWidthLocation, 150, buttonWidth, buttonHeight);
        roptions = new Rectangle(buttonWidthLocation, 300, buttonWidth, buttonHeight);
        rhelp = new Rectangle(buttonWidthLocation, 450, buttonWidth, buttonHeight);
        rmainMenu = new Rectangle(buttonWidthLocation, 600, buttonWidth, buttonHeight);

        play.setBounds(rplay);
        options.setBounds(roptions);
        help.setBounds(rhelp);
        mainMenu.setBounds(rmainMenu);

        add(play);
        add(options);
        add(help);
        add(mainMenu);

        play.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Frame frame = new Frame();
                //need to remove this panel here
                frame.ThreeD();
            }
        });
        options.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("options");
            }
        });
        help.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("help");
            }
        });
        mainMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("mainMenu");
            }
        });
    }
}

And this is my Frame class:

package Run;

import javax.swing.*;

import ThreeD.Display;
import ThreeD.Launcher;
import TowerDefence.Window;


import java.awt.*;
import java.awt.image.BufferedImage;

public class Frame extends JFrame{

    public static String title = "Game";        

    /*public static int GetScreenWorkingWidth() {
        return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
    }*/

    /*public static int GetScreenWorkingHeight() {
        return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
    }*/

    //public static Dimension size = new Dimension(GetScreenWorkingWidth(), GetScreenWorkingHeight());
    public static Dimension size = new Dimension(1280, 774);


    public static void main(String args[]) {
        Frame frame = new Frame();

        System.out.println("Width of the Frame Size is "+size.width+" pixels");
        System.out.println("Height of the Frame Size is "+size.height+" pixels");
    }

    public Frame() {
        setTitle(title);
        setSize(size);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ThreeDLauncher();
    }

    public void ThreeDLauncher() {      
        Launcher launcher = new Launcher();
        add(launcher);

        setVisible(true);       
    }

    public void TowerDefence() {
        setLayout(new GridLayout(1, 1, 0, 0));

        Window window = new Window(this);
        add(window);

        setVisible(true);
    }

    public void ThreeD() {
        BufferedImage cursor = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(0, 0), "blank");

        getContentPane().setCursor(blank);

        Display display = new Display();
        add(display);

        setVisible(true);

        display.start();
    }

}
  • 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-04T11:10:11+00:00Added an answer on June 4, 2026 at 11:10 am

    Basically – you are creating new instance of Frame in line:

    Frame frame = new Frame(); //referencing to my JFrame class (this class is a JPanel)
    

    New instance of Frame is not visible, and you’re try to remove your Launcher from not visible new Frame. But this is wrong – you should remove Launcher from Frame that you created previously in main function (that is: parent of Launcher component).

    Here goes an example:

    public class TestFrame extends JFrame {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    TestFrame frame = new TestFrame();
                    frame.getContentPane().add(new MyPanel(frame));
                    frame.setSize(200, 200);
                    frame.setLocationRelativeTo(null);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
    
        }
    }
    

    And MyPanel class:

    public class MyPanel extends JPanel {
        public MyPanel(final TestFrame frame) {
            JButton b = new JButton("Play");
            add(b);
            b.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Container pane = frame.getContentPane();
                    pane.remove(MyPanel.this);
                    JPanel otherPanel = new JPanel();
                    otherPanel.add(new JLabel("OtherPanel"));
                    pane.add(otherPanel);
                    pane.revalidate();
                }
            });
        }
    }
    

    In your example you should add a reference to Frame in your Launcher constructor:

    public Launcher(Frame frame) {
        this.frame = frame;
        ...
    

    Init Launcher:

    public void ThreeDLauncher() {     
        Launcher launcher = new Launcher(this);
    

    and use:

        play.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //need to remove this panel here
                frame.getContentPane().remove(Launcher.this);
                frame.ThreeD();
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to remove a given assembly (log4net.dll in this case, but it should
I'm trying to remove everything from a string that is NOT an html tag
I'm trying to remove some deprecated code from a site. Can anyone tell me
I am trying to remove a file from path using following code. But my
I trying to remove a cookie in a servlet with this code Cookie minIdCookie
Trying to remove all letters and characters that are not 0-9 and a period.
I'm trying to remove all but the first child component from a Java Container.
I am trying to remove a directory with rmdir , but I received the
I'm trying to remove an object from (inside?) a object literal. But I cant
I am trying to remove a few top-level menus on the WordPress admin panel.

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.