aspose file tools*
The moose likes Groovy and the fly likes How does inner class access outter class functions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "How does inner class access outter class functions?" Watch "How does inner class access outter class functions?" New topic
Author

How does inner class access outter class functions?

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 381
This program works. How can I eliminate the reference "me" on line 6 and use line 10 instead of line 9 to add the button? I think line 10 should work but it causes a syntax error.
Thanks!
Siegfried

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Try replacing "this.ButtonDemo" with "ButtonDemo.this".
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 381
Great! Now it works in Java with your suggested change!

Why does it not work in groovy console 1.7.8 with your suggested change?

Why does line 9 work in java but not groovy 1.7.8?

Thanks,
Siegfried
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
I don't know. It works fine in Groovy 1.7.5 using groovyc. Well, you also have to fix the comment on line 20, but that was true in Java as well. What error message do you get? Maybe there's a bug in 1.7.8.
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 381
I get the same error from groovyc and groovyconsole.


Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
I ran this past a Groovy-oriented co-worker, and we found that (a) earlier versions of Groovy didn't really support nested/inner classes at all, (b) the current Groovy versions mostly support them now, but (c) the OuterClass.this syntax is still not supported by Groovy. Maybe it will be in the future, but so far, no.

Looking further at your code, it's not clear why you might need this anyway. You basically are doing three things with the button:

1. Create a button.
2. Add it to the JPanel
3. Add an action listener.

There's no reason this all has to be done inside an instance initializer of an anonymous class, though. That's just needlessly confusing to me. (And to Groovy, apparently.) How about:

I also removed the stuff about doing everything in the event dispatch thread. That seems unnecessary given that the button hasn't been displayed yet - that's the last thing that happens in this code. It makes little difference in this app, but in general I would avoid putting too much in the event dispatch thread - at least, unnecessary stuff - because that's what slows down your app's reaction to everything else that might happen.

My co-worker also offered this more Groovy version of the code (does not work as Java):

But really, if you're using Groovy, you probably want SwingBuilder - you code will be much nicer.
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 381

Thank you very much!
Yes -- I've been trying to get my head around swingbuilder for a while.

OK, we are continuing to talk about Groovy here instead of Java....
I don't understand how that "as ActionListener" works when we only give it one function.
Can you translate this into groovy for me so I can better understand how that works?

Thanks,
siegfried

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Siegfried Heintze wrote:I don't understand how that "as ActionListener" works when we only give it one function.

Well, ActionListener only has one method, so I guess Groovy translates the one code block into an ActionListener implementation that uses the code block as the implementation of the one method. If ActionListener had more than one method, this probably wouldn't work.

I'm not really a Groovy programmer though, so maybe it would be better to ask these questions in the CodeRanch Groovy forum. Or perhaps some nice moderator-type will move the thread over there for you.
James Ervin
Greenhorn

Joined: Aug 18, 2010
Posts: 1
Mike Simmons wrote:
Siegfried Heintze wrote:I don't understand how that "as ActionListener" works when we only give it one function.

Well, ActionListener only has one method, so I guess Groovy translates the one code block into an ActionListener implementation that uses the code block as the implementation of the one method. If ActionListener had more than one method, this probably wouldn't work.

I'm not really a Groovy programmer though, so maybe it would be better to ask these questions in the CodeRanch Groovy forum. Or perhaps some nice moderator-type will move the thread over there for you.


I'm the Groovy Oriented Co-worker and what Mike describes is essentially true.

In the case where you have an interface that has more than one method you can do the following. You would take a map with the keys as the method names, the values as a closure representing the method body and then use the 'as' operator to coerce it into the appropriate interface. This method is really cool for quick stubbing in tests.

"Noli nothis permittere te terere."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does inner class access outter class functions?