0

I am new to GUI Creation and having the following problem:

I have a main class: AppWindow.java containing a JFrame.

If a Button is pressed the class new.java which extends JPanel is added to the upper JFrame. Working fine so far.

The problem now is, that the JPanel should be removed, when a Button in JPanel is pressed. How can I do that?

My Idea: The JFame must somehow listen to the JPanel, whether it may be removed.

Thank you!

SwingNoob
  • 33
  • 7
  • Use `getParent` from the `JPanel`, it will return the parent `Container` or `null` if the panel is not contained within any container. Use `Container#remove` (passing it `this`) to remove the panel from the container – MadProgrammer Aug 08 '15 at 05:05
  • 2
    Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556) instead of all this adding & removing stuff. – Andrew Thompson Aug 08 '15 at 05:12
  • Is there any really good tutorial out there which explains all that stuff well? So i do the following? `codeJPanel o = (JPanel) new.this.getParent(); o.remove(Neu.this); o.repaint();` – SwingNoob Aug 08 '15 at 05:24
  • I noticed that having the Panel removed by itself makes a central organisaton difficult. The main Frame should do the action handling shouldn it? So any Panels must somehow notify the AppWindow Class what their current state is. – SwingNoob Aug 08 '15 at 05:27
  • @SwingNoob That comes down to you choice of design. By removing the component from the parent and calling `revalidate` on it (the parent), it gets notified – MadProgrammer Aug 08 '15 at 06:34
  • What would be a good Design? What organisation can handle big complexity? The user should be able to stay in the main window all the time. He can access multiple functions via menus which then will be displayed in the main window. – SwingNoob Aug 08 '15 at 07:22

1 Answers1

0

Add this line to the pressed button code,and replace YourJpanelName with panel or frame name which you want to removed when button pressed.

YourJpanelName.setVisible(false);