views:

90

answers:

3

I would like to reduce the vertical size of a JButton. The following code works fine for K > 1 but I can't seem to reduce the size. Any suggestions?

JButton button = /* ... get button here ... */
Dimension d = button.getPreferredSize();
d.setSize(d.getWidth(), d.getHeight()*K);
button.setPreferredSize(d);

edit: I'm using JavaBuilders + MigLayout. It looks like I have to do button.setMaxSize(d); instead of setPreferredSize(), not sure why.

+4  A: 
aioobe
+1 More flexible solution! Please, no `7f` size fonts. :-)
trashgod
+2  A: 

As an alternative, some L&Fs (e.g. Nimbus, Aqua) support a JComponent.sizeVariant, as discussed in Resizing a Component and Using Client Properties.

trashgod
+1 Great solution!
aioobe
+2  A: 

Maybe just play with the Border of the button:

Insets insets = button.getInsets();
insets.top = 0;
insets.bottom = 0;
button.setMargin( insets );
camickr