From: Andrew Thompson on
On Feb 17, 7:40 am, Fencer <no.i.d...(a)want.mail.from.spammers.com>
wrote:
>...One question about my program code, ..

Famous last words. ;)

>...if I want to make the buttons have
> the same width, how should I do that? ...

Put the components in a GridLayout in a JPanel.
Add that JPanel to the GridBagLayout.

And as an aside: What is it with GBL experts where
they feel they have to prove that 'anything is possible
with GBL'? If there is a layout better suited to
part of a GUI, use it!

--
Andrew T.
pscode.org
From: RedGrittyBrick on
On 16/02/2010 21:04, markspace wrote:
> Fencer wrote:
>
>> One question about my program code, if I want to make the buttons have
>> the same width, how should I do that?
>
>
> I just click the "same size" button. (In the code below, I think that's
> implemented with the jPanel1Layout.linkSize(...) call.)
>

/**
* Make some buttons all be the same width:
*
* <pre>
* Utils.equalizeButtonWidths(new JButton[] { addButton,
* editButton, removeButton,
* printButton, viewButton });
* </pre>
*
* @param buttons
* an array of JButtons
*/
public static void equalizeButtonWidths(JButton[] buttons) {
int maxWidth = 0;
int maxHeight = 0;
for (JButton b : buttons) {
Dimension size = b.getPreferredSize();
int w = size.width;
int h = size.height;
if (w > maxWidth)
maxWidth = w;
if (h > maxHeight)
maxHeight = h;
}
Dimension buttonSize = new Dimension(maxWidth, maxHeight);
for (JButton b : buttons) {
b.setMinimumSize(buttonSize);
b.setPreferredSize(buttonSize);
b.setMaximumSize(buttonSize);
}
}