user3552407
user3552407

Reputation: 371

GridBagLayout: Swing

Mine is a very basic doubt regarding GridBaGLayout in Swing. I am trying to first divide the given frame in three parts with weighty as (0.2, 0.2 and 0.6). The code below, works fine as I see three portions with the respective weights.

        JFrame frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        JPanel p1= new JPanel(), p2 = new JPanel(), p3 = new JPanel();


    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;

    c.weightx = 1;
    c.weighty = 0.2;
    c.gridx = 0;
    c.gridy = 0;
    frame.add(p1,c);
    p1.setBackground(Color.lightGray);

    c.gridy = 1;
    frame.add(p2,c);
    p2.setBackground(Color.gray);

    c.weighty = 0.6;
    c.gridy = 2;
    frame.add(p3,c);
    p3.setBackground(Color.blue);

Next I try to add a JLabel in panel p1. Like this: p1.add(new JLabel("Hi")). If I observe the opened frame before and after adding, I see the difference in the weights allotted in two cases. First (when JLabel is not added), 0.2,0.2 and 0.6 are allotted. Second, when JLabel is added, first panel increases by a bit and second decreases by a bit. You can see in the below pic also:

enter image description here

What is the reason for this? As I think it has got something to do with the layout assigning weights as per the available space in the parent frame or panel. But don't quite understand the reasoning properly.
The weightx/y division is based on the available space. Then how panel p1 and p2 get almost the same height? Let the frame's height be h. When p1 is added, available height is h, then p1 gets 0.2*h which leaves us with available as 0.8h. Now when p2 has to be added, 0.2 of 0.8h will be given to it which is 0.16h. Then how p1 and p2 (in light gray and dark gray background colors) appear to be of the same height?

Also, how can I achieve the constant division and keep the panels of the same weights as they are assigned?

Upvotes: 1

Views: 57

Answers (1)

camickr
camickr

Reputation: 324118

I am trying to first divide the given frame in three parts with weighty as (0.2, 0.2 and 0.6).

The "weightx/y" constraints are for "extra" space.

Each component is give space at its preferred size. Then if the frame size increases, each component is given extra space in the specified ratio. So unless the preferred size of each component is also in the specified ratio the components will not have the overall desired ratio.

The height of an empty JPanel is 10 (based on the default size calculation of the FlowLayout). So the preferred height of all 3 panels is 30.

If you have a content pane of size 130 then you have 100 pixels of extra space so the sizes of the panels become:

p1 = 10 + (100 * .2) = 30
p2 = 10 + (100 * .2) = 30
p3 = 10 + (100 * .6) = 70

When you add a label to p1 then the preferred height becomes 10 plus the height of the label, lets say (10 + 15) = 25. So now the preferred height of the 3 panels is (25 + 10 + 10) = 45. So there is only 85 pixels to be divided in your specified ratio.

how can I achieve the constant division and keep the panels of the same weights as they are assigned?

You can check out Relative Layout. It was designed to allow you to specify the relative size for each component.

Upvotes: 2

Related Questions