3 Replies Latest reply on Oct 9, 2004 4:02 AM by 3004

    Add and remove  panel below a main top pane without affecting the top pane

      I am trying to write a Swing based application with a main frame containing 2 panels:
      - a main top panel (always visible)
      - a bottom panel that comes and goes.

      When the user clicks on a button named "Show Cache", I want the bottom panel to show up below the main panel. When the user clicks on another button "Hide Cache", I want the new panel to disappear.
      Whether the bottom panel is visible or not, I do not want the appearance of the top panel to change.

      I have tried many things but I can't get it to work properly:

      1st problem: typically, when I click on "Show Cache", the frame does not get larger and the bottom panels fills the space inside the frame, leaving a little room above for the main panel.

      2nd problem: Now, if I enlarge the window by dragging down its south border, everything looks fine. However, when I click "HIde Cache", the bottom pane disappears as expected, but the freed space is filled by the top main pane, which becomes much larger than at the beginning. I want the freed space to vanish.

      I have tried setVisible(true/false), I have tried setSize on the panel to resize it, I have tried add and remove on the new panel... Nothing works fine. Each time the top pane is affected by the creation of the bottom pane or by its disappearance.

      Thanks in advance for your help.


        • 1. Re: Add and remove  panel below a main top pane without affecting the top pane
          try this
          import javax.swing.*;
          import java.awt.*;
          import java.awt.event.*;
          public class Frame1 extends JFrame {
            JButton southComponent = new JButton();
            JButton centerComponent = new JButton();
            Dimension withoutBottom = new Dimension(400,250);
            Dimension withBottom = new Dimension(400,300);
            JPanel jPanel1 = new JPanel();
            JButton btnShow = new JButton();
            JButton btnHide = new JButton();
            Container content;
            public Frame1() {
              content = this.getContentPane();
              southComponent.setText("Bottom Component");
              this.getContentPane().setLayout(new BorderLayout());
              centerComponent.setText("Center Component");
              this.getContentPane().add(southComponent,  BorderLayout.SOUTH);
              this.getContentPane().add(centerComponent,  BorderLayout.CENTER);
              this.getContentPane().add(jPanel1, BorderLayout.NORTH);
              jPanel1.add(btnShow, null);
              jPanel1.add(btnHide, null);
              btnHide.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                  getContentPane().add(centerComponent,  BorderLayout.CENTER);
                  getContentPane().add(jPanel1, BorderLayout.NORTH);
              btnShow.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                  getContentPane().add(southComponent,  BorderLayout.SOUTH);
                  getContentPane().add(centerComponent,  BorderLayout.CENTER);
                  getContentPane().add(jPanel1, BorderLayout.NORTH);
            public static void main(String[] args) {
              Frame1 frame1 = new Frame1();
              frame1.setSize(new Dimension(400,300));
          • 2. Re: Add and remove  panel below a main top pane without affecting the top pane
            Instead of using setSize(...) to resize your frame after a component has been added/removed, you could also use frame.pack().
            • 3. Re: Add and remove  panel below a main top pane without affecting the top pane
              Thanks. Repack was the solution and works fine. Sorry for the duplicate post that was not malicious.