my dog learned polymorphism
The moose likes Swing / AWT / SWT and the fly likes JButton, icons and size. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton, icons and size." Watch "JButton, icons and size." New topic

JButton, icons and size.

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Hi !
I have the follow problem.I have a button with a icon(gif) and no text, I set the icon with : button.setIcon(....).The icon size is 16,16 but my button is a little bit bigger that my icon.

What I need a "small" button - with the same size with its icon.

A solution is to use setPreferredSize method on my button(the pref. size is the icon size) but I not so "happy" with this.
The logic behind this is : if the button content(picure and/or text) size is smaller than the preffered size than the last one(pref. size) is used, the button takes its icon size only if the icon is bigger that its preffered size.

There are also other solutions/comments?

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

You need to use setMargin(Insets). You will need to create an Insets object with values of like 1 or 0 for the amount of the inset so that your border is small or nill. I had to do this for JToolBar menus because, as you have found out, the margins are just too large.

GenRocket - Experts at Building Test Data
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

ThanX a lot,it works and is also very logic.
I agree. Here's the link:
subject: JButton, icons and size.
It's not a secret anymore!