-1

I'm trying to implement a button to make both my JFrame and JPanel bigger (by the same amount), but it doesn't quite seem to work.

myPanel.setPreferredSize(new Dimension(50, 50));
button1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
      getContentPane().remove(myPanel);
      myPanel.invalidate();

      myPanel.setMinimumSize(new Dimension(myPanelWidth, myPanelHeight+100));
      myPanel.setBounds(new Rectangle(myPanel.getBounds().getX(), myPanel.getBounds.getY(), myPanel.getBounds().getWidth(), myPanel.getBounds().getHeight+100));
      getContentPane().setBounds(new Rectangle(myFrameX, myFrameY, myFrameWidth, myFrameHeight+100));
      getContentPane().add(myPanel, BorderLayout.SOUTH);
      System.out.println(myPanel.getBounds().getHeight());

      myPanel.validate();
      myPanel.repaint();

The problem is that my frame resizes properly - it becomes 100 units bigger, but the panel doesn't. I'm not sure I'm validating and repainting properly. What am I doing wrong?

Edit: Fixed! I needed a setPreferredSize() update inside of my ActionListener.

MikeM
  • 342
  • 2
  • 10

1 Answers1

0

Nevermind, I fixed it!! Outside of the button's ActionListener I had a myPanel.setPreferredSize(...) call. I had to update this preferred size inside of the ActionListener, i.e.

myPanel.setPreferredSize(new Dimension(myPanelWidth, myPanelHeight+100));

Thanks!

MikeM
  • 342
  • 2
  • 10
  • 3
    [Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod Jun 05 '14 at 14:01