Search code examples
javaswingjpanellayout-managerborder-layout

Java JPanel expand one panel in BorderLayout


I have two simple JPanels, one positioned on top and the other on the bottom.

Now im trying to resize the top panel to take up more space (and therefore push the bottom panel down)

Ive added a button where i listen on call like:

  button.addMouseListener(new MouseAdapter() {  
      public void mouseReleased(MouseEvent e) {   
          panelTop.setSize(10, 200);
      }  
  });

This is how i add my panels:

add(panelTop, BorderLayout.NORTH);
add(panelBottom, BorderLayout.SOUTH);

Any ideas if this is possible?

My tests are not working at all


Solution

  • You should use setPreferredSize instead of setSize. The BorderLayout will notice this and resize the panel on the next validate().