• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How does inner class access outter class functions?

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try replacing "this.ButtonDemo" with "ButtonDemo.this".
 
Siegfried Heintze
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get the same error from groovyc and groovyconsole.


 
Mike Simmons
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic