Resizing a button?

R

Ryan Stewart

I have two JButtons on a JPanel that uses BoxLayout.Y_AXIS, and I'd like to
make them the same size. I've tried setSize(Dimension d), setSize(int width,
int height) and setPreferredSize(Dimension d). The only one that did
anything was the setPreferredSize. It didn't do a thing to the buttons, but
it made the JPanel (which is the West part of a BorderLayout) wider. How can
I specify a size for these buttons?
 
K

Knute Johnson

Ryan said:
I have two JButtons on a JPanel that uses BoxLayout.Y_AXIS, and I'd like to
make them the same size. I've tried setSize(Dimension d), setSize(int width,
int height) and setPreferredSize(Dimension d). The only one that did
anything was the setPreferredSize. It didn't do a thing to the buttons, but
it made the JPanel (which is the West part of a BorderLayout) wider. How can
I specify a size for these buttons?

You need to use a different layout manager. A GridLayout would make all
the components the same size. A BoxLayout only resizes components in
one direction.
 
S

Steve Claflin

Knute said:
You need to use a different layout manager. A GridLayout would make all
the components the same size. A BoxLayout only resizes components in
one direction.

--

Knute Johnson
email s/nospam/knute/
Molon labe...

The other thing you might try is to setMinimumSize and setMaximumSize
all to the same size as the preferred size -- although that isn't
guaranteed to work if there are other constraints on the overall layout.
 
R

Ryan Stewart

Steve Claflin said:
The other thing you might try is to setMinimumSize and setMaximumSize
all to the same size as the preferred size -- although that isn't
guaranteed to work if there are other constraints on the overall layout.

Thanks for those suggestions, but neither creates the effect I'm looking
for. Setting the min and max size actually has no discernable effect on a
BoxLayout. The GridLayout halfway works, but the problem is that I don't
want the buttons to change size when the window is resized. I need a way to
create a single column of buttons all the same size, but a static size. That
has to be possible, doesn't it?
 
S

Steve Claflin

Ryan said:
Thanks for those suggestions, but neither creates the effect I'm looking
for. Setting the min and max size actually has no discernable effect on a
BoxLayout. The GridLayout halfway works, but the problem is that I don't
want the buttons to change size when the window is resized. I need a way to
create a single column of buttons all the same size, but a static size. That
has to be possible, doesn't it?

Well, you could place your GridLayout container in a JPanel, and then
put the JPanel into whatever is holding the whole thing.
 
R

Ryan Stewart

Steve Claflin said:
Well, you could place your GridLayout container in a JPanel, and then
put the JPanel into whatever is holding the whole thing.
That's what I have. I tried a GridLayout with one column of ten rows on a
JPanel in the West portion of a BorderLayout. When the window is 500 x 400,
my default starting size, it looks fine. If it's resized to maybe 200 x 200,
the buttons are ridiculously small, and if it's maximized they're huge.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top