File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to get image (icon) from a JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to get image (icon) from a JButton" Watch "How to get image (icon) from a JButton" New topic
Author

How to get image (icon) from a JButton

prakash chauhan
Ranch Hand

Joined: Jun 22, 2007
Posts: 81
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

Joined: Jun 13, 2009
Posts: 2182
    
    7
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

Joined: Sep 06, 2009
Posts: 15
Hello

Try this:


Good Luck and Happy Coding.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
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

Joined: Sep 06, 2009
Posts: 15
Gotcha, and Thank You Sir for the tips
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get image (icon) from a JButton