aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Image as a button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image as a button" Watch "Image as a button" New topic
Author

Image as a button

kenji jonathan
Greenhorn

Joined: Oct 07, 2011
Posts: 4

I want to make an image that can act like a button in java application.

I have written :



but the result is there is an image inside a button. It isn't what I want to do, I want to make an image that acts like a button...Any suggestion?...Thanks for sharing knowledge..^^
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Use a JLabel instead of a JButton, and use a mouse listener. You can also change the cursor of the label if you want to.

I'll move this thread to our GUI forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Welcome to the Ranch.

Or you can also
1) With your existing code, make your button transparent by using JButton#setContentAreaFilled(false)
2) Refer to http://www.coderanch.com/t/506289/GUI/java/custom-shaped-gui-components and http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Minh Nam
Ranch Hand

Joined: Sep 10, 2011
Posts: 57
kenji jonathan wrote:I want to make an image that can act like a button in java application.

I have written :



but the result is there is an image inside a button. It isn't what I want to do, I want to make an image that acts like a button...Any suggestion?...Thanks for sharing knowledge..^^


This is what I wrote to make a transparent button having only the icon:



Advanced java topics
kenji jonathan
Greenhorn

Joined: Oct 07, 2011
Posts: 4

Wow..thanks for sharing...by the way, now I want to place the button at the center of the window. Is there any simple way to do it?....
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

kenji jonathan wrote:I want to place the button at the center of the window. Is there any simple way to do it?....


If the button is the only component in its Container, set a GridBagLayout and add the button without a GridBagConstraints.


luck, db
There are no new questions, but there may be new answers.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Image as a button