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

def swing = new SwingBuilder()
count = 0
def textlabel
def frame = swing.frame(title:'Frame', size:[300,300]) {
  borderLayout()
  textlabel = label(text:"Clicked ${count} time(s).",
                    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()

...

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
def textlabel
def frame = swing.frame(title:'Frame', defaultCloseOperation:JFrame.EXIT_ON_CLOSE, pack:true, show:true) {
    vbox {
        textlabel = label("Clicked ${count} time(s).Click the button!")
        button(
            text:'Click Me',
            actionPerformed: {
                count++
                textlabel.text = "Clicked ${count} time(s)."
                println "Clicked!"
            }
        )
        widget(sharedPanel())
        widget(sharedPanel())
    }
}

...

Code Block
import javax.swing.*
import javax.swing.tree.DefaultMutableTreeNode as TreeNode
import groovy.swing.SwingBuilder

mboxes = [
    [name: "root@example.com", folders: [[name: "Inbox"], [name: "Trash"]]],
    [name: "test@foo.com", folders: [[name: "Inbox"], [name: "Trash"]]]
]

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())def swing = new SwingBuilder()
JTree mboxTree
new SwingBuilder()swing.frame(title: 'Mailer', defaultCloseOperation: JFrame.DISPOSE_ON_CLOSE,
    size: [800, 600], show: true, locationRelativeTo: null) {
    lookAndFeel("system")
    menuBar() {
        menu(text: "File", mnemonic: 'F') {
            menuItem(text: "Exit", mnemonic: 'X', actionPerformed: {dispose() })
        }
    }
    splitPane {
        scrollPane(constraints: "left", preferredSize: [160, -1]) {
            mboxTree = tree(rootVisible: false)
        }
        splitPane(orientation:JSplitPane.VERTICAL_SPLIT, dividerLocation:280) {
            scrollPane(constraints: "top") { mailTable = table() }
            scrollPane(constraints: "bottom") { textArea() }
        }
    }
    ["From", "Date", "Subject"].each { mailTable.model.addColumn(it) }
}

mboxTree.model.root.removeAllChildren()
mboxes.each {mbox ->
  DefaultMutableTreeNode  def node = new DefaultMutableTreeNodeTreeNode(mbox.name)
    mbox.folders.each { folder -> node.add(new DefaultMutableTreeNodeTreeNode(folder.name)) }
    mboxTree.model.root.add(node)
}
mboxTree.model.reload(mboxTree.model.root)

...

@Bindable and @Vetoable AST transformations

Although not specific to the Swing builderSwingBuilder, Groovy 1.6 introduced two Bindable and Vetoable transformation of interest to Swing developers.