Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>SwingBuilder allows you to create full-fledged Swing GUIs in a declarative and concise fashion. It accomplishes this by employing a common idiom in Groovy, builders. Builders handle the busywork of creating complex objects for you, such as instantiating children, calling Swing methods, and attaching these children to their parents. As a consequence, your code is much more readable and maintainable, while still allowing you access to the full range of Swing components.</p> <p>Here's a simple example of using SwingBuilder:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import groovy.swing.SwingBuilder import java.awt.BorderLayout as BL count = 0 new SwingBuilder().edt { 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) } } </pre></td></tr></table> <p>Here is what it will look like:</p> <p><img class="confluence-embedded-image" src="/download/attachments/231080130/SwingBuilder001.gif?version=1&modificationDate=1369369603366" data-image-src="/download/attachments/231080130/SwingBuilder001.gif?version=1&modificationDate=1369369603366" data-linked-resource-id="231375631" data-linked-resource-type="attachment" data-linked-resource-default-alias="SwingBuilder001.gif" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231080130" title="null > SwingBuilder001.gif"></p> <p>This hierarchy of components would normally be created through a series of repetitive instantiations, setters, and finally attaching this child to its respective parent. Using SwingBuilder, however, allows you to define this hierarchy in its native form, which makes the interface design understandable simply by reading the code.</p> <p>The flexibility shown here is made possible by leveraging the many programming features built-in to Groovy, such as closures, implicit constructor calling, import aliasing, and string interpolation. Of course, these do not have to be fully understood in order to use SwingBuilder; as you can see from the code above, their uses are intuitive.</p> <p>Here is a slightly more involved example, with an example of SwingBuilder code re-use via a closure.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import groovy.swing.SwingBuilder import javax.swing.* import java.awt.* def swing = new SwingBuilder() def sharedPanel = { swing.panel() { label("Shared Panel") } } count = 0 swing.edt { 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()) } } } </pre></td></tr></table> <p>Here's another variation that relies on observable beans and binding</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 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: [100, 100], 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++ }) } } </pre></td></tr></table> <p><a href="http://groovy.codehaus.org/Bindable+and+Vetoable+transformation">@Bindable</a> is one of the core <a href="http://groovy.codehaus.org/Compile-time+Metaprogramming+-+AST+Transformations">AST Transformations</a>. 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.</p> <h3>Mailer User Interface example</h3> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 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"]]] ] def swing = new SwingBuilder() JTree mboxTree 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 -> def node = new TreeNode(mbox.name) mbox.folders.each { folder -> node.add(new TreeNode(folder.name)) } mboxTree.model.root.add(node) } mboxTree.model.reload(mboxTree.model.root) </pre></td></tr></table> <ul> <li><a class="confluence-link" href="/display/GROOVY/Alphabetical+Widgets+List" data-linked-resource-id="78170" data-linked-resource-type="page" data-linked-resource-default-alias="Alphabetical Widgets List" data-base-url="http://docs.codehaus.org">Alphabetical Widgets List</a></li> <li><a class="confluence-link" href="/display/GROOVY/Categorical+Widget+List" data-linked-resource-id="78178" data-linked-resource-type="page" data-linked-resource-default-alias="Categorical Widget List" data-base-url="http://docs.codehaus.org">Categorical Widget List</a></li> <li><a class="confluence-link" href="/display/GROOVY/Extending+Swing+Builder" data-linked-resource-id="78185" data-linked-resource-type="page" data-linked-resource-default-alias="Extending Swing Builder" data-base-url="http://docs.codehaus.org">Extending Swing Builder</a></li> <li><a class="confluence-link" href="/display/GROOVY/Multithreading+with+SwingBuilder" data-linked-resource-id="111706636" data-linked-resource-type="page" data-linked-resource-default-alias="Multithreading with SwingBuilder" data-base-url="http://docs.codehaus.org">Multithreading with SwingBuilder</a></li> </ul> <h2>@Bindable and @Vetoable AST transformations</h2> <p>Although not specific to SwingBuilder, Groovy 1.6 introduced two <a class="confluence-link" href="/display/GROOVY/Bindable+and+Vetoable+transformation" data-linked-resource-id="117900711" data-linked-resource-type="page" data-linked-resource-default-alias="Bindable and Vetoable transformation" data-base-url="http://docs.codehaus.org">Bindable and Vetoable transformation</a> of interest to Swing developers. </p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced