Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get image (icon) from a JButton

 
prakash chauhan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can we get the image(in png,gif,jpeg)from the icon that is set on a JButton using the setIcon() method?
I need to retrieve this image and save it in any of the availble file formats(jpg,png or gif)
Here is my sample code


So basically I want to retrieve "javalog.gif" and save it somewhere.
I tried to use some Image.getImage() method but dint know how to proceed further.
Any help is much appreciated
Thanks.
 
Rob Camick
Ranch Hand
Posts: 2608
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the ImageIconAPI for a method to get the image.

Then you can use the ImageIO class to write the image to a file.
 
Laythe Chamse
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Try this:


Good Luck and Happy Coding.
 
Rob Camick
Ranch Hand
Posts: 2608
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, that is NOT a very good example:

a) You should NOT be overriding the paint() method of JFrame.
b) There is no need to do custom painting you can just add an Icon to a label and add the label to the frame
c) If you do need custom painting then you should be overriding the paintComponent() method NOT the paint() method of a JPanel or JComponent.

Read the section from the Swing tutorial for more information about Icons and Custom Painting.
 
Laythe Chamse
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotcha, and Thank You Sir for the tips
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic