12

I want to have a resizable panel, that always has the top green panel of a fixed depth. i.e. all changes in height should effect the yellow panel only.

My code below is almost OK, except the green panel varies in size a little.

How do I do this?

            Panel.setLayout(new BoxLayout(Panel, BoxLayout.Y_AXIS));
        Panel.setAlignmentX(Component.LEFT_ALIGNMENT);

        JPanel TopPanel = new JPanel();
        TopPanel.setPreferredSize(new Dimension(80,150));
        TopPanel.setVisible(true);
        TopPanel.setBackground(Color.GREEN);
        JPanel MainPanel = new JPanel();
        MainPanel.setPreferredSize(new Dimension(80,750));
        MainPanel.setVisible(true);
        MainPanel.setOpaque(true);
        MainPanel.setBackground(Color.YELLOW);

        Panel.add(TopPanel);
        Panel.add(MainPanel);

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ManInMoon
  • 6,795
  • 15
  • 70
  • 133

2 Answers2

14

Your question didn't restrict the solution to a BoxLayout, so I am going to suggest a different layout manager.

I would attack this with a BorderLayout and put the green panel in the PAGE_START location. Then put the yellow panel in the CENTER location without a preferredSize call.

http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

Here is an SSCCE example of the solution:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestPad extends JFrame {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new BorderLayout());

        JPanel green = new JPanel();
        green.setPreferredSize(new Dimension(80, 150));
        green.setBackground(Color.GREEN);

        JPanel yellow = new JPanel();
        yellow.setBackground(Color.YELLOW);

        frame.getContentPane().add(green, BorderLayout.PAGE_START);
        frame.getContentPane().add(yellow, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}
jzd
  • 23,473
  • 9
  • 54
  • 76
  • See comment for gla3dr – ManInMoon Nov 12 '13 at 18:20
  • @ManInMoon, works fine for me, I have edited by answer with a full example. There must be something different you are doing. – jzd Nov 12 '13 at 18:58
  • +1. This helped me fix an issue where I originally had 3 components in a VerticalBox and a change to one would cause all 3 to change size. Now, changes to one don't affect the size of others due to using a BorderLayout. – James Birch Jan 25 '19 at 14:44
3

If you make your Panel use BorderLayout instead of BoxLayout and put TopPanel in BorderLayout.NORTH and MainPanel in BorderLayout.CENTER, then they will both resize horizontally, but only the MainPanel will resize vertically.

See the BorderLayout documentation

gla3dr
  • 2,179
  • 16
  • 29
  • That doesn't appear correct, green and yellow panels remain the same height RELATIVE to each other, BUT green does grow and shrink - I was hoping to fix its height – ManInMoon Nov 12 '13 at 18:04