Versions Compared

Key

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

...

Code Block
import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL

defcount swing = 0
new SwingBuilder().edt count{
= 0 def textlabel
def frame = swing.frame(title:'Frame', size:[300,300], show: true) {
    borderLayout()
    textlabel = label(text:"Click the button!", constraints: BL.NORTH)
    button(text:'Click Me',
         actionPerformed: {count++; textlabel.text = "Clicked ${count} time(s)."; println "clicked"},
         constraints:BL.SOUTH)
  }
frame.show()}

Here is what it will look like:

...

Code Block
import groovy.swing.SwingBuilder
import javax.swing.*
import java.awt.*

def swing = new SwingBuilder()

def sharedPanel = {
     swing.panel() {
        label("Shared Panel")
    }
}

count = 0
defswing.edt textlabel{
def frame = swing. frame(title:'Frame', defaultCloseOperation:JFrame.EXIT_ON_CLOSE, pack:true, show:true) {
        vbox {
            textlabel = label("Click the button!")
            button(
                text:'Click Me',
                actionPerformed: {
                    count++
                    textlabel.text = "Clicked ${count} time(s)."
                    println "Clicked!"
                }
            )
            widget(sharedPanel())
            widget(sharedPanel())
        }
    }
}

Here's another variation that relies on observable beans and binding

Code Block

import groovy.swing.SwingBuilder
import groovy.beans.Bindable

class MyModel {
   @Bindable int count = 0
}

def model = new MyModel()
new SwingBuilder().edt {
  frame(title: "Java Frame", size: [300, 300], locationRelativeTo: null, show: true) {
    gridLayout(cols: 1, rows: 2) {
      label(text: bind(source: model, sourceProperty: "count", converter: { v ->  v? "Clicked $v times": ''})
      button("Click me!", actionPerformed: { model.count++ })
    }
  }
} 

@Bindable is one of the core AST Transformations. It generates all the required boilerplate code to turn a simple bean into an observable one. The bind() node creates appropriate PropertyChangeListeners that will update the interested parties whenever a PropertyChangeevent is fired.

Mailer User Interface example

...