> I am trying to build a 1 line JPanel with a mix of Labels and buttons of
> varying sizes. I was trying to do it with GridBagLayout but it appears
> that GridBag loses its variable column spanning abilities with a single
> row. Below is a sample program. If it is run "as is", two buttons will
> appear even sized, even though one is directed to span 2 columns.
The number of columns spanned does not directly control
the column width. If you want the preferred width of the
second button to be double what it would normally be, then
do this:
button = new JButton("5") {
public Dimension getPreferredSize() {
Dimension ps = super.getPreferredSize();
ps.width *= 2;
return ps; } };
If you want it to be exactly double the width of the first,
then use a final variable to hold the first button and do this:
button = new JButton("5") {
public Dimension getPreferredSize() {
Dimension ps = button1.getPreferredSize();
ps.width *= 2;
return ps; } };
But you probably don't want to do either of those. For most
layouts you will have either a vertical or horizontal line of
buttons, all of which you want to be the same width - the
preferred width of the widest one. GridBagLayout will work
for a column of same-width buttons, but not, in any nice
way, for a row of same-width buttons.
Lou Lipnickey - 18 Jan 2005 23:56 GMT
figures .... thanks - Lou
>>I am trying to build a 1 line JPanel with a mix of Labels and buttons of
>>varying sizes. I was trying to do it with GridBagLayout but it appears
[quoted text clipped - 28 lines]
> for a column of same-width buttons, but not, in any nice
> way, for a row of same-width buttons.