This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jbutton background image" Watch "jbutton background image" New topic
Author

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.

Thanks,
Cathy.
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
 
 
subject: jbutton background image
 
Similar Threads
Icons
How to get image (icon) from a JButton
Image as a button
Painting an image on a button
How to stretch icon to fit on JToggleButton