2

I have an 3 * 6 array of JButtons inside a GridBagLayout. But since the text length of each button may vary, each column of buttons has slightly different width. How do I make them have the same width?

Here is the code:

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 6; j++)
    {
        buttonConstraints.fill = GridBagConstraints.BOTH;
        buttonConstraints.weightx = 1;
        buttonConstraints.weighty = 1;
        buttonConstraints.gridx = j;
        buttonConstraints.gridy = i;
        buttonPanel.add(buttons[i * 6 + j], buttonConstraints);
    }
}

Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
tianz
  • 2,155
  • 2
  • 19
  • 28

3 Answers3

5

But since the text length of each button may vary, each column of buttons has slightly different width. How do I make them have the same width?

  • simple solution is to use GridLayout

  • GridBagLayout to create a columns, Dimension for each of column is calculated from PrefferedSize came from every JButtons in 1st row, these coordinates are used for rest of rows in the current container

EDIT

enter image description here

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridFromGridBagLayout {

    private JFrame frame = new JFrame();
    private JPanel panel = new JPanel();

    public GridFromGridBagLayout() {
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        for (int row = 0; row < 6; row++) {
            for (int col = 0; col < 3; col++) {
                JButton b = new JButton("(" + row + ", " + col + ")");
                b.putClientProperty("column", col);
                b.putClientProperty("row", row);
                b.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JButton btn = (JButton) e.getSource();
                        System.out.println("clicked column "
                                + btn.getClientProperty("column")
                                + ", row " + btn.getClientProperty("row"));
                    }
                });
                gbc.gridx = col;
                gbc.gridy = row;
                gbc.gridwidth = gbc.gridheight = 1;
                gbc.fill = GridBagConstraints.BOTH;
                gbc.anchor = GridBagConstraints.NORTHWEST;
                gbc.weightx = 33;
                gbc.weighty = 20;
                panel.add(b, gbc);
            }
        }
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                GridFromGridBagLayout borderPanels = new GridFromGridBagLayout();
            }
        });
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

With the help of using GridBagConstraints you can set the width equal or as you want. follow this link for help:

GridBagConstraints

  • 1
    Can you please be more specific? Which attribute of GridBagConstraints should I set? I've tried both weightx and gridwidth, but they don't work. Thank you. – tianz Apr 25 '13 at 10:20
-2

try this:

buttonConstraints.fill = GridBagConstraints.HORIZONTAL;
Fredrick Gauss
  • 5,126
  • 1
  • 28
  • 44
Izanagi
  • 19
  • 1