This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does inner class access outter class functions?

 
Siegfried Heintze
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try replacing "this.ButtonDemo" with "ButtonDemo.this".
 
Siegfried Heintze
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the same error from groovyc and groovyconsole.


 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic