Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

This page is obsolete, newer versions of Groovy have support for inner classes


Groovy 1.0 and 1.1 do not support normal Java inner classes (you can have such classes only in Scripts or you can code them in Java of course).

In many cases though, you don't need them. Here is a Groovy 1.1 example which shows you how to construct a class on the fly which implements several interfaces. This would be a prime example where you would typically use an inner class in Java. In Groovy we don't have to:

Code Block

import groovy.swing.SwingBuilder
import static java.awt.BorderLayout.*
import java.awt.event.*

//  set up variables
count = 0
def textlabel
def text = "Actions: "
def update = { c ->
    text += c
    textlabel.text = text

// create the listener
def closureMap = [
    mousePressed:     { update 'M' },
    keyPressed:       { update 'K' },
    focusLost:        { update 'F' },
    windowIconified:  { update 'W' }
def interfaces = [WindowListener, KeyListener, MouseListener, FocusListener]
def listener = ProxyGenerator.instantiateAggregate(closureMap, interfaces)

// now the GUI
def swing = new SwingBuilder()
def frame = swing.frame(title:'Frame') {
    textlabel = label(text:text, constraints: NORTH)
    button = button(text:'Click Me', constraints: SOUTH)
frame.addWindowListener listener
['Key', 'Mouse', 'Focus'].each {
    button."add${it}Listener" listener
    textlabel."add${it}Listener" listener