It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Image in place of  buttons 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 in place of  buttons" Watch "Image in place of  buttons" New topic

Image in place of buttons

Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
I wanna use image as buttons in place of simple buttons provided by java.awt package, in addition to this, I also wanna change mouse cursor to 'hand' whenever the mouse has come over that image. But dont know how to go about all this. Can anyone help me out !!!
Regards and thanx in advance,

Best wishes,
Jahanzeb Sayal

Joined: Mar 02, 2001
Posts: 21
Images(Icons) cant be displayed in Button class provided by java.awt package because it is an OS widget . The most you can try is that u can extend Button class & override its paint method draw image there it will be shown instead of rectangular button but i havent tested it in detail & i got poor results you better use swing JButton.
You can change the curosr on the button by using setCursor(Cursor cursor) of the Component class super class of every component in java.
Check java documentation for details.

[This message has been edited by Jahanzeb Sayal (edited April 21, 2001).]

Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
Using swing is totally out of consideration as my clients are all web based and even the latest versions of popular browsers doesnt support swing. The clients will need to download the plugin first and then only they can acces my swing enabled site.
Is there some other way out ?
regards and thanks ,
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

I have previously posted code to how to make an image button in this thread...
As far as changing the cursor goes... simply add a MouseListener to your button and do a setCursor( Cursor c) on a mouseEntered event, and set it back to the old cursor on the mouseExited event... look at the Cursor class in the API to find out what cursor you need to send to setCurser to get the hand... I think it is Cursor.HAND_CURSOR...
So, you would have something like this in your mouse listener...

[This message has been edited by Nathan Pruett (edited April 23, 2001).]

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Sam,
java.awt does not support the icon property associated with
a button. However, the same functionality can be done with swing's JButton.

Please note an extra space after the < applet tag and < /applet tag .

[This message has been edited by Ravindra Mohan (edited April 24, 2001).]
I agree. Here's the link:
subject: Image in place of buttons
It's not a secret anymore!