Can components of a gridbaglayout fill parent frame upon resize?
Asked Answered
K

2

8

I've got a JFrame whose root JPanel is instantiated with a GridBagLayout. At runtime, the panel is populated with components based on some description, and the width, height and x,y coords are given in the description to be used with the gridwidth, gridheight, gridx and gridy fields in GridBagConstraints. The components themselves can be also be JPanels with their own child components and GridBagConstraints, the GUI is described in a tree so Frame is populated recursively.

The problem I'm having is that when the frame is resized, the inner components are not stretched to fill their given widths and heights. I've given an example of the layout code below, with screenshots.

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.*;

public class GridBagTest extends JPanel {

    private final GridBagConstraints gbc = new GridBagConstraints();

    public GridBagTest(){

        setLayout(new GridBagLayout());
        add(gbcComponent(0,0,1,2), gbc);    
        add(gbcComponent(1,0,2,1), gbc);            
        add(gbcComponent(1,1,1,1), gbc);            
        add(gbcComponent(2,1,1,1), gbc);            

    }

    //Returns a JPanel with some component inside it, and sets the GBC fields
    private JPanel gbcComponent(int x, int y, int w, int h){

        gbc.gridx = x; 
        gbc.gridy = y;
        gbc.gridwidth = w;
        gbc.gridheight = h;
        gbc.fill = GridBagConstraints.BOTH;
        JPanel panel = new JPanel();
        JTextField text = new JTextField("(" + w + ", " + h + ")");
        panel.setBorder(new TitledBorder("(" + x + ", " + y + ")"));        
        panel.add(text);
        return panel;

    }

    public static void main (String args[]){

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new GridBagTest());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

When running it After stretching

I need it so in picture 2, the inner JPanels holding the components are resized to fill their respective widths and heights on the GridBagLayout, ideally stretching their components as well.

Kare answered 13/7, 2014 at 15:0 Comment(0)
E
16

This is usually due to your not setting weightx and weighty constraints.

Try:

gbc.gridx = x; 
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;

// **** add here:
gbc.weightx = 1.0;
gbc.weighty = 1.0;

This will allow your GUI to expand in the x and y directions if need be.

Elmer answered 13/7, 2014 at 15:7 Comment(3)
Looks like this works! Can't believe I've never tried it before. Now is there any way that inner panels that only have one component has that component stretch in size to fill it? i.e. the widths of the textfields are stretched?Kare
@adnan_252: yes, but you'll need to give those inner JPanels layout managers that allow this such as GridBagLayout, or BorderLayout, or... Currently your inner JPanels use just the default FlowLayout, and this layout is quite simple and does not stretch out components.Elmer
@adnan_252: you're welcome. And thanks for posting a minimal example program with your questino.Elmer
D
0

You can set columnWidths and rowHeights on resize of your JPanel. Look at this sample. I modified your code for two columns case:

public GridBagTest(){

    setLayout(new GridBagLayout());
    add(gbcComponent(0,0,1,2), gbc);    
    add(gbcComponent(1,0,1,1), gbc);

    addComponentListener(new java.awt.event.ComponentAdapter() {
        @Override
        public void componentResized(java.awt.event.ComponentEvent evt) {
            GridBagLayout layout = (GridBagLayout) getLayout();
            JPanel panel = (JPanel)evt.getComponent();
            int width = panel.getWidth();
            int height = panel.getHeight();
            int wGap = panel.getInsets().left+panel.getInsets().right;
            int hGap = panel.getInsets().top+panel.getInsets().bottom;
            layout.columnWidths = new int[]{width/2-wGap, width/2-wGap};
            layout.rowHeights = new int[]{height-hGap};
        }
    });

}

Depending on your columns and rows count, you can change it. Here is the result: enter image description here enter image description here

Doronicum answered 4/1, 2016 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.