Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the component size with GridLayout? Is there a better way?

I am working on a larger GUI with Java and I am becoming angry on Layout Managers.

I have a "Settings-Panel" with a variable number of JComponents in it (Labels, Buttons, JSpinners, JSliders,...). I just want the following:

JLabel <-> JComponent

JLabel <-> JComponent

JLabel <-> JComponent

...

My Panel has a size of 500px, so that there is enough space for a lot of components. Unfortunatly the GridLayout always stretches the size of the Components to the whole Panel, even if I set a MaximumSize for every component. It looks stupid if there are only two buttons each with a height of 250px.

I tried flow Layout, but i cannot figure out a way to make new lines properly. I tried BoxLayout.Y_AXIS, but the Components are always centered, and Label and Component are not in the same line.

Does anybody know a good and short way with LayoutManagers to handle this properly?

like image 677
Blackbam Avatar asked Jan 15 '11 13:01

Blackbam


2 Answers

An alternative to other layouts, might be to put your panel with the GridLayout, inside another panel that is a FlowLayout. That way your spacing will be intact but will not expand across the entire available space.

like image 119
jzd Avatar answered Sep 22 '22 14:09

jzd


Don't use GridLayout for something it wasn't meant to do. It sounds to me like GridBagLayout would be a better fit for you, either that or MigLayout (though you'll have to download that first since it's not part of standard Java). Either that or combine layout managers such as BoxLayout for the lines and GridLayout to hold all the rows.

For example, using GridBagLayout:

import java.awt.*;
import javax.swing.*;

public class LayoutEg1 extends JPanel{
    private static final int ROWS = 10;

    public LayoutEg1() {
        setLayout(new GridBagLayout());
        for (int i = 0; i < ROWS; i++) {
            GridBagConstraints gbc = makeGbc(0, i);
            JLabel label = new JLabel("Row Label " + (i + 1));
            add(label, gbc);

            JPanel panel = new JPanel();
            panel.add(new JCheckBox("check box"));
            panel.add(new JTextField(10));
            panel.add(new JButton("Button"));
            panel.setBorder(BorderFactory.createEtchedBorder());
            gbc = makeGbc(1, i);
            add(panel, gbc);
        }
    }

    private GridBagConstraints makeGbc(int x, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.weightx = x;
        gbc.weighty = 1.0;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = (x == 0) ? GridBagConstraints.LINE_START : GridBagConstraints.LINE_END;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        return gbc;
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("Layout Eg1");
        frame.getContentPane().add(new LayoutEg1());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}
like image 28
Hovercraft Full Of Eels Avatar answered Sep 21 '22 14:09

Hovercraft Full Of Eels