0

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:

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Use GridLayout if you want the components to all be the same size. Remember that you can nest JPanels, each using its own layout manager. – Hovercraft Full Of Eels Jan 24 '17 at 23:48
  • `GridBagConstraints#fill`? – MadProgrammer Jan 24 '17 at 23:48
  • That's a big frame for just two buttons. Do more components need to go in there? Provide ASCII art or a simple drawing of the *intended* layout of the (entire) GUI at minimum size, and if resizable, with more width and height. `button1.setSize(new Dimension(200,200));` A layout manager will more likely honour the preferred size than the size, but don't set that either, since the preferred size will be no better than a guess. – Andrew Thompson Jan 24 '17 at 23:55
  • You might also like to have a look at [How to use GridBagLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html), one of the first examples looks like it doing what you want – MadProgrammer Jan 24 '17 at 23:56
  • Posible duplicate of [*How to increase size of JRadioButtons & JCheckBoxes?*](http://stackoverflow.com/q/6258402/230513) – trashgod Jan 25 '17 at 07:00
  • I edited my post with a picture how it should look like – JonDoeMaths Jan 25 '17 at 10:27
  • Tip: Add @HovercraftFullOfEels (or whoever, the `@` is important) to *notify* the person of a new comment. As mentioned by HFoE, this is well suited to a single column `GridLayout` Provide some layout component spacing and add a largish empty border to the panel that contains the button, 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. – Andrew Thompson Jan 25 '17 at 14:56
  • What @AndrewThompson said. – Hovercraft Full Of Eels Jan 25 '17 at 16:53

1 Answers1

2

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433