Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

def// swing =set newup SwingBuilder()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') {
    borderLayout()
    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
}
frame.pack()
frame.show()