File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes jbutton background image 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 background image" Watch "jbutton background image" New topic

jbutton background image

Cathy Song
Ranch Hand

Joined: Jul 01, 2003
Posts: 92
Hi All,

This code adds an image on top of jbutton:
Icon icon = new ImageIcon ("oval.png");
JButton back = new JButton(icon);

How can I change the size of the button so that it fits the image size.
Basically I am trying to create an oval button.

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Usually a JButton should show up at the correct size to show all the image when you instantiate it with the ImageIcon. If you change the icon later you may have to call 'revalidate' on the button or its parent container. If the button is in a Container whose layout manager respects the preferred size of its components, eg, FlowLayout, GridBagLayout, this should work

You can use 'setBounds' if you need to get heavy-handed.
[ August 08, 2005: Message edited by: Craig Wood ]
Mahesh x Bogadi
Ranch Hand

Joined: Jul 06, 2004
Posts: 51
use setPreferredSize(new Dimension(width, height))
Cathy Song
Ranch Hand

Joined: Jul 01, 2003
Posts: 92
Thanks Craig and Mahesh - that solution worked
I agree. Here's the link:
subject: jbutton background image
It's not a secret anymore!