Replacing a JButton with an Image
Hey, after a lot of reading/googling I managed to figure out how to use Swing a little bit; my latest question which I've been unable to find an answer for (I've probably found the answer somewhere but didn't know I had) is if it's possible to replace a JButton with an image.
I'm pretty much just messing around creating frames, panels, buttons and that and I thought it might be cool to experiment with custom buttons.
Thanks in advance for any help!
Re: Replacing a JButton with an Image
JButtons can accept Images held by ImageIcons by simply calling setIcon(Icon icon) on the JButton. If you don't want the button's border, it can be removed as well.
Re: Replacing a JButton with an Image
Quote:
Originally Posted by
curmudgeon
JButtons can accept Images held by ImageIcons by simply calling setIcon(Icon icon) on the JButton. If you don't want the button's border, it can be removed as well.
I've seen the setIcon() used in a few of the pages I checked out when googling for a solution but they always only showed a picture being placed within the button, not replacing the whole button. Just to clarify will setIcon() be able to replace the entire space that the button takes up with an image?
Re: Replacing a JButton with an Image
Try using a JLabel instead of a JButton to hold the image.
Re: Replacing a JButton with an Image
Quote:
Originally Posted by
tyeeeee1
I've seen the setIcon() used in a few of the pages I checked out when googling for a solution but they always only showed a picture being placed within the button, not replacing the whole button. Just to clarify will setIcon() be able to replace the entire space that the button takes up with an image?
As Norm suggests, you can use a JLabel, or if you still want to use a JButton for its added functionality (mouse-over, change in icons based on state, ability to disable it), then yes the entire button can be replaced with an image, though you have to do some manipulation of it (that I don't fully 100% remember at this time), such as possibly not painting the borders and setting the insets to 0. If you search though you will find exactly what needs to be done.
Re: Replacing a JButton with an Image
Thanks for the help, I'll look into this a bit more and see if I can get what I'm picturing to work.