aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes EventHandler as ActionListener usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "EventHandler as ActionListener usage" Watch "EventHandler as ActionListener usage" New topic
Author

EventHandler as ActionListener usage

Marcus Kelvin
Ranch Hand

Joined: Jan 04, 2012
Posts: 44
There's something I don't get:



When I click on the button, I get:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: No method called foo on class Whatever with no arguments


Yet it seems to me that my invocation is identical to one of the examples in the oracle api docs for EventHandler...and if foo() isn't a method of Whatever that takes no arguments, then what is it? :/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

Class Whatever needs to be public, which means it has to move into a file of its own. I tried that and it worked.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Marcus Kelvin
Ranch Hand

Joined: Jan 04, 2012
Posts: 44
Rob Spoor wrote:Class Whatever needs to be public, which means it has to move into a file of its own. I tried that and it worked.


O_o Just out of curiosity: does anyone know why this should be the case? It seems a bad choice design wise (can't use non-public inner class instances as a target!), so I presume/hope that it is technical... while there are other ways to accomplish the same thing, doesn't this make "EventHandler" a limp biscuit?

Sorry if that sounds a little ticked off, lol. The oracle docs are generally pretty good -- I don't see why they wouldn't make this explicit.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
If you search, you find lots of threads mentioning the rule that a public class and its file must have the same name. Example.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

I think that what Marcus means is that why the class needs to be public.

What I have experienced is that reflection has trouble finding non-public classes in general. This only applies to the classes themselves though. If a public class is nested in a non-public class (and is static) then it can be loaded through reflection. I've done this myself just quite recently. I was using some tool that automatically generated the public class, and I couldn't add a nested class to it. I could add code outside the class. What I did was create one non-public class with a public classes nested in it, and that worked just fine.

Yes, you can use nested classes as targets. Just make sure they are static nested classes:
Marcus Kelvin
Ranch Hand

Joined: Jan 04, 2012
Posts: 44
Rob Spoor wrote:I think that what Marcus means is that why the class needs to be public.
Yeah.

What I have experienced is that reflection has trouble finding non-public classes in general. This only applies to the classes themselves though. If a public class is nested in a non-public class (and is static) then it can be loaded through reflection. I've done this myself just quite recently. I was using some tool that automatically generated the public class, and I couldn't add a nested class to it. I could add code outside the class. What I did was create one non-public class with a public classes nested in it, and that worked just fine.


Again it would be nice if exactly what the rules are were documented -- maybe the official specification defines this more clearly (if EventListener or java.reflect is in the specification)? Not something I've looked at much yet.

What's interesting in that light is that the first version of that code didn't use EventListener; it used class.getMethod() and java.lang.reflect.Method and passed the same (non-public) class instance and Method reference into the listening class, which then used Method.invoke() in actionPerformed(). That worked...maybe because the Method reference was assigned in main?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

I tried to see how EventHandler works, but in the end it passes control to a class that's not part of the official API. It did not use simple reflection up to that point though, so perhaps the non-API class (com.sun.beans.finder.MethodFinder) also checks if the class the method belongs to is public.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EventHandler as ActionListener usage