Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Replacing a JButton with an Image

  1. #1
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default 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!


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default 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.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    tyeeeee1 (December 4th, 2012)

  4. #3
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: Replacing a JButton with an Image

    Quote Originally Posted by curmudgeon View Post
    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?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,212
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: Replacing a JButton with an Image

    Try using a JLabel instead of a JButton to hold the image.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    tyeeeee1 (December 4th, 2012)

  7. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Replacing a JButton with an Image

    Quote Originally Posted by tyeeeee1 View Post
    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.

  8. The Following User Says Thank You to curmudgeon For This Useful Post:

    tyeeeee1 (December 4th, 2012)

  9. #6
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default 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.

Similar Threads

  1. Replacing red with a picture
    By antnas in forum Object Oriented Programming
    Replies: 5
    Last Post: November 8th, 2012, 04:28 PM
  2. Replacing File Content
    By asheshrocky in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 30th, 2012, 09:48 PM
  3. How to save Image into a package by JButton save?
    By justyStepi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2012, 07:02 PM
  4. [SOLVED] Replacing dots with spaces
    By dTi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 17th, 2011, 07:53 PM
  5. [SOLVED] replacing subString
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 21st, 2010, 09:47 PM