Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

key button help

hi all,
jus a simple question, how would i code a button so that if the NUMPAD 0 was pressed, it would change the buttons icon? i am currently using an image for the button and wish to change that image to a different upon select.
thanks
java newbie
[277 byte] By [aj_306] at [2007-11-11 7:26:40]
# 1 Re: key button help
I assume the problem is the image switching, not catching the Numpad 0 keystroke.

The hard way:
Make a JButton extension (ImageButton ?), introduce a new constructor that has two Images
as parameters. Then override the paintComponent(Graphics g) method in the
JButton. In this method you paint one or the other image, based on a flag that is
set by a method you include in the ImageButton class, say, setImage(boolean onOff). This method is invoked on every Numpad 0 keystroke.

The easy way:

Make your button a JToggleButton, and set the images using the setSelectedIcon and
the setIcon methods. Switch image by invoking the setSelected method for the button.
sjalle at 2007-11-11 22:38:20 >
# 2 Re: key button help
thanks for that mate! i took the asy option lol!!!

on the off chance can u recommend any easy, Zoom, pan and rotate methods / code that i can implement for my buttons? i have zoom code but its very comprehensive!!!

if not its ok thanks for ur help... much appreciated!
aj_306 at 2007-11-11 22:39:20 >
# 3 Re: key button help
Check this out

http://www.apl.jhu.edu/~hall/java/Java2D-Tutorial.html

and here is a sample applet that I made a while back, it zooms and pans, and, well,
its a bit comprehensive, but it doesnt do anything unnecessary...
sjalle at 2007-11-11 22:40:18 >
# 4 Re: key button help
thanks alot sjalle!

i will have a look at the code, i am not getting no image up tho!

mate u have really helped me alot, its very decent of u, thanks! :)
aj_306 at 2007-11-11 22:41:18 >
# 5 Re: key button help
i have compiled ur zoom applet but it says that there are "none" image/s to display... where do i enter the image path?
aj_306 at 2007-11-11 22:42:28 >
# 6 Re: key button help
hello?
aj_306 at 2007-11-11 22:43:27 >
# 7 Re: key button help
i think i may have to start a new topic?
aj_306 at 2007-11-11 22:44:25 >