JonDoeMaths
JonDoeMaths

Reputation: 23

Making JButtons equal size using GridBagLayout

I want to know if it is possible to set the size of JButton components in a GridBagLayout, I want my to Buttons "Start" and "Quit" among each other, and I want to make them bigger, the size is irrelevant, I want to know the general procedure.

I tried different things I found on the Internet (using other layouts) but it just ended with more errors.

public class Display implements Runnable
{

    public Display()
    {

    }

    @Override
    public void run() {

        JFrame frame = new JFrame();
        frame.setTitle("Title");
        frame.setPreferredSize(new Dimension(500,700));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        createComponents(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }

    private void createComponents(Container cont)
    {

        GridBagLayout pane = new GridBagLayout();
        JButton button1 = new JButton("Start");
        JButton button2 = new JButton("Quit");

        button1.setSize(new Dimension(200,200));
        button1.setAlignmentX(Component.CENTER_ALIGNMENT);
        button2.setAlignmentX(Component.CENTER_ALIGNMENT);


        cont.setLayout(pane);
        cont.add(button1);
        cont.add(button2);

    }

    public static void main(String[] args)
    {

        Display d = new Display();
        SwingUtilities.invokeLater(d);

    }
}

How it should look:

Upvotes: 0

Views: 822

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168825

As mentioned by Hovercraft Full Of Eels, this is well suited to a single column GridLayout. A grid layout makes all components the same size. Provide some layout component spacing and add a largish empty border to the panel that contains the buttons, and the job is done.

..and I want to make them (buttons) bigger

There are a number of ways to make buttons larger. e.g.

  1. Set a larger font.
  2. Call setMargin(Insets) to add more space around the text.
  3. Use a large icon.
  4. Make the text longer (for more width).

The 3rd and 4th ways are quite arbitrary.


This example uses the first two, as well as an empty border around the buttons to provide further white-space.

enter image description here

Check the comments in the code for details.

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

public class ButtonColumnLayout {

    private JComponent ui = null;
    String[] labels = {"Start", "Stop", "Quit"};
    // adjust numbers to change spacing between button text and button edge
    Insets insets = new Insets(10,40,10,40);

    ButtonColumnLayout() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        // adjust last two numbers to change spacing between buttons
        ui = new JPanel(new GridLayout(0, 1, 10, 10));
        // adjust numbers to change border around buttons
        ui.setBorder(new EmptyBorder(40,100,40,100));

        for (String s : labels) {
            ui.add(getBigButton(s));
        }
    }

    private final JButton getBigButton(String text) {
        JButton b = new JButton(text);
        // adjust float value to change font size
        b.setFont(b.getFont().deriveFont(25f));
        b.setMargin(insets);

        return b;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                ButtonColumnLayout o = new ButtonColumnLayout();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Upvotes: 2

Related Questions