0

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • What is in these panels? Explain in the form of an [SSCCE](http://sscce.org/). I feel the current answers, while correct, are less than optimal. Will know more when I see the SSCCE. – Andrew Thompson Jul 25 '13 at 12:06

2 Answers2

5

What you are trying to do can be easily solved with a JSplitPane

This will take two components and put a divider between the two that you can use to re-size them.

Rob Wagner
  • 4,391
  • 15
  • 24
1

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

tbodt
  • 16,609
  • 6
  • 58
  • 83
  • Some good reason to override `etPreferredSize()` are suggested [here](http://stackoverflow.com/q/7229226/230513). – trashgod Jul 25 '13 at 14:14