PDA

View Full Version : Listener question


Bender
12-21-2002, 03:26 PM
What kind of listener would I use for a JLabel if I wanted to listen for a user to click on it?

I can't use ActionListener... so what can I use?

Thanks.

Dru Lee Parsec
12-22-2002, 02:26 PM
You could add a mouseListener and then catch the mouseClicked event. Or you could use a JButton instead of a JLabel BUT set the border to an emptyBorder so it looks like a JLabel. Then you can add an action listener.

Bender
12-22-2002, 02:31 PM
I ended up using a MouseListener (I guess the "MouseClicked" event was too obvious for me. :))

Here's another question though. Is it possible to create an abstract, anonymous class so that I can only define the MouseClicked method without defining some empty MousePresssed, MouseReleased blah blah blah methods?

Thanks.

stuka
12-23-2002, 01:12 AM
OK, it's been to long since I did this, but there are some abstract classes out there for just this purpose. It's Mouse{something} - it MIGHT be MouseAdapter (in fact now I think that's right) that does just that.

Dru Lee Parsec
12-23-2002, 12:33 PM
Here's another question though. Is it possible to create an abstract, anonymous class so that I can only define the MouseClicked method without defining some empty MousePresssed, MouseReleased blah blah blah methods?

Yep, it's MouseAdapter

stuka
12-23-2002, 12:55 PM
*whew* Glad I got it right :D

Bender
12-23-2002, 03:47 PM
Thanks for the help guys. Works great. But just for my general education, is it at all possible to create abstract anonymous classes? I doubt it, as I can't even imagine how I'd go about it, but I just want to make sure.

And here's another question to keep your Java minds busy.

Against my better judgement, I'm turning an application I wrote into an applet. It runs fine as an application, but when I try to run it as an applet I get a "java.security.AccessControlException: access denied (java.io.FilePermission" error, which I guess means I'm not allowed to access files (to read) in an applet? That can't be right.

Any ideas?

stuka
12-23-2002, 04:07 PM
That sounds just about right - applets typically aren't allowed access to the end user's file system for security reasons (especially for MS systems). As for anonymous abstract classes, how would that work? You'd have to extend it, which would just be UGLY IMO.

Bender
12-23-2002, 04:30 PM
Originally posted by Stuka
That sounds just about right - applets typically aren't allowed access to the end user's file system for security reasons (especially for MS systems). As for anonymous abstract classes, how would that work? You'd have to extend it, which would just be UGLY IMO. All I'm doing, though, is creating an ImageIcon with an image file. The image is stored on the same machine that the HTML file is served from, not from a remote user machine. That's where I get my error. Basically I'm doing:

JLabel something = new JLabel(new ImageIcon("images//pic.gif"));

which is where I get the error. Where can I put the images to have access to them?

stuka
12-30-2002, 11:35 AM
Applets run on the client side - your applet thinks it's looking for an image on the user's machine, not on the server. I'm not familiar enough with the applet stuff to know how to do what you want, but IIRC you can put your applet into a jar file, and work with it that way - maybe you can load your images into the jar too, and pull it from there?