Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up a maximum component size when using GridBagLayout in java

My problem is the following : I'm trying to have a JScrollPane resizing with the window, up to a certain size horizontally, where it should stop trying to grow with the window.

Can I do that with a GridBagLayout ? If so, how ?

like image 979
Thomas P Avatar asked Mar 01 '13 15:03

Thomas P


People also ask

What does the GridBagLayout class allow you?

GridBagLayout is one of the most flexible — and complex — layout managers the Java platform provides. A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height.

What is the difference between GridLayout and GridBagLayout manager?

A GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size.

How do you use insets in GridBagLayout?

Try something like this: GridBagLayout gbl=new GridBagLayout(); setLayout(gbl); GridBagConstraints gbc=new GridBagConstraints(); gbc. insets = new Insets(10, 10, 10, 10); JLabel jl = new JLabel("This is a JLabel!", SwingConstants. CENTER); jl.

What is weight in GridBagLayout?

GridBagConstraints.weightx , GridBagConstraints.weighty. Used to determine how to distribute space, which is important for specifying resizing behavior. Unless you specify a weight for at least one component in a row ( weightx ) and column ( weighty ), all the components clump together in the center of their container.


1 Answers

One way to do that is to wrap you scrollpane in another JPanel with a BoxLayout and set a MaximumSize on your scrollpane which BoxLayout will enforce:

Packed:

packed display

Stretched (max width has been set to 700 px): stretched display

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class TestGridBagLayout2 {

    protected void initUI() throws MalformedURLException {
        final JFrame frame = new JFrame();
        frame.setTitle(TestGridBagLayout2.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JPanel panel = new JPanel(new GridBagLayout());
        Vector<Vector<String>> data = new Vector<Vector<String>>();
        for (int i = 0; i < 20; i++) {
            Vector<String> v = new Vector<String>();
            for (int j = 0; j < 1; j++) {
                v.add("Cell (" + (i + 1) + "," + (j + 1) + ")");
            }
            data.add(v);
        }
        DefaultTableModel model = new DefaultTableModel(data, new Vector<String>(
                Arrays.asList("Col-1"/*, "Col-2", "Col-3", "Col-4", "Col-5"*/)));
        JTable table = new JTable(model);
        JScrollPane scroll = new JScrollPane(table);
        scroll.setMaximumSize(new Dimension(700, Integer.MAX_VALUE));
        JPanel wrappingPanel = new JPanel(null);
        wrappingPanel.setLayout(new BoxLayout(wrappingPanel, BoxLayout.LINE_AXIS));
        wrappingPanel.add(scroll);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        panel.add(wrappingPanel, gbc);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new TestGridBagLayout2().initUI();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
like image 159
Guillaume Polet Avatar answered Nov 02 '22 12:11

Guillaume Polet