SwingBuilder for creating Swing GUIs in Groovy - provides DSL for declarative UI construction
npx @tessl/cli install tessl/maven-org-apache-groovy--groovy-swing@5.0.0Groovy Swing provides a comprehensive domain-specific language (DSL) for building Java Swing user interfaces declaratively using Groovy's builder pattern. It eliminates the need for verbose imperative Java Swing code, offering factories for all major Swing components, data binding capabilities, and advanced features like custom renderers and layout managers.
implementation 'org.apache.groovy:groovy-swing:5.0.0'import groovy.swing.SwingBuilder
import groovy.swing.LookAndFeelHelperFor Java integration:
import groovy.swing.SwingBuilder;
import groovy.swing.LookAndFeelHelper;import groovy.swing.SwingBuilder
import javax.swing.*
def swing = new SwingBuilder()
def frame = swing.frame(title: 'Hello World', defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {
panel {
borderLayout()
label(text: 'Hello, Swing!', constraints: BorderLayout.CENTER)
button(text: 'Click Me', constraints: BorderLayout.SOUTH) {
actionPerformed {
JOptionPane.showMessageDialog(null, 'Button clicked!')
}
}
}
}
frame.pack()
frame.visible = trueGroovy Swing is built around several key components:
The main SwingBuilder class and fundamental DSL capabilities for creating UI components, managing look and feel, and handling threading.
class SwingBuilder extends FactoryBuilderSupport {
SwingBuilder(boolean init = true)
// Threading methods
def edt(Closure c)
def doLater(Closure c)
def doOutside(Closure c)
// Static factory methods
static def edtBuilder(Closure c)
static def lookAndFeel(Object laf, Closure initCode = null)
static def lookAndFeel(Map attributes = [:], Object laf = null, Closure initCode = null)
// Utility methods
def build(Closure c)
def shortcut(key, modifier = 0)
def createKeyStrokeAction(Map attributes, JComponent component = null)
}
class LookAndFeelHelper {
static LookAndFeelHelper getInstance()
static String getNimbusLAFName()
static String getAquaLAFName()
def addLookAndFeelAlias(String alias, String className)
def lookAndFeel(Object value, Map attributes, Closure initClosure)
}Factory-based DSL keywords for creating all major Swing UI components including windows, buttons, text fields, and containers.
// Window components
def frame(Map attributes = [:], Closure closure = null)
def dialog(Map attributes = [:], Closure closure = null)
def window(Map attributes = [:], Closure closure = null)
def fileChooser(Map attributes = [:])
def optionPane(Map attributes = [:])
// Button components
def button(Map attributes = [:], Closure closure = null)
def checkBox(Map attributes = [:], Closure closure = null)
def radioButton(Map attributes = [:], Closure closure = null)
def toggleButton(Map attributes = [:], Closure closure = null)
// Text components
def textField(Map attributes = [:], Closure closure = null)
def textArea(Map attributes = [:], Closure closure = null)
def label(Map attributes = [:], Closure closure = null)
def passwordField(Map attributes = [:], Closure closure = null)
def formattedTextField(Map attributes = [:], Closure closure = null)
def editorPane(Map attributes = [:], Closure closure = null)
def textPane(Map attributes = [:], Closure closure = null)
// Container components
def panel(Map attributes = [:], Closure closure = null)
def scrollPane(Map attributes = [:], Closure closure = null)
def tabbedPane(Map attributes = [:], Closure closure = null)
def desktopPane(Map attributes = [:])
def layeredPane(Map attributes = [:])
def viewport(Map attributes = [:], Closure closure = null)
def toolBar(Map attributes = [:], Closure closure = null)
// Selection components
def list(Map attributes = [:], Closure closure = null)
def comboBox(Map attributes = [:], Closure closure = null)
def tree(Map attributes = [:])
def slider(Map attributes = [:])
def spinner(Map attributes = [:])
def colorChooser(Map attributes = [:])
def separator(Map attributes = [:])
def progressBar(Map attributes = [:])
def scrollBar(Map attributes = [:])
// Generic widget factories
def widget(Map attributes = [:], Closure closure = null)
def container(Map attributes = [:], Closure closure = null)
def bean(Map attributes = [:], Closure closure = null)Layout manager factories for organizing components including BorderLayout, GridLayout, BoxLayout, and advanced layouts like GridBagLayout and TableLayout.
// Layout managers
def borderLayout(Map attributes = [:])
def flowLayout(Map attributes = [:])
def gridLayout(Map attributes = [:])
def gridBagLayout(Map attributes = [:])
def boxLayout(Map attributes = [:])
// Box layout components
def hbox(Map attributes = [:], Closure closure = null)
def vbox(Map attributes = [:], Closure closure = null)
def hglue()
def vglue()
def rigidArea(Map attributes = [:])
// Table layout
def tableLayout(Map attributes = [:])
def tr(Map attributes = [:], Closure closure = null)
def td(Map attributes = [:], Closure closure = null)Support utilities for actions, collections, resources, and keyboard shortcuts.
// Actions and resources
def action(Map attributes = [:])
def actions(List actionList)
def imageIcon(Map attributes = [:])
def buttonGroup(Map attributes = [:])
// Collections and utilities
def map(Map attributes = [:])
def noparent(List items)
def actions(List actionList)
// Keyboard shortcuts
def keyStrokeAction(Map attributes, JComponent component = null)Comprehensive data binding system with ValueModel interfaces, property binding, and automatic UI synchronization with data models.
// Binding factories
def bind(Map attributes = [:])
def bindGroup(Map attributes = [:])
def bindProxy(Map attributes = [:])
// Data models
interface ValueModel {
Object getValue()
void setValue(Object value)
Class getType()
boolean isEditable()
}
class ValueHolder implements ValueModel {
ValueHolder()
ValueHolder(Object value)
ValueHolder(Object value, Class type)
}
class PropertyModel implements ValueModel {
PropertyModel(Object bean, String propertyName)
}Enhanced table components with custom models, column definitions, renderers, and sorters for building data-driven table interfaces.
// Table components
def table(Map attributes = [:], Closure closure = null)
def tableModel(Map attributes = [:], Closure closure = null)
def tableColumn(Map attributes = [:])
def columnModel(Map attributes = [:])
def column(Map attributes = [:])
// Column types
def propertyColumn(Map attributes = [:])
def closureColumn(Map attributes = [:])
// Renderers and editors
def tableCellRenderer(Map attributes = [:])
def listCellRenderer(Map attributes = [:])
def cellRenderer(Map attributes = [:])
def headerRenderer(Map attributes = [:])
def onRender(Closure renderClosure)
def cellEditor(Map attributes = [:])
def editorValue(Closure valueClosure)
def prepareEditor(Closure prepareClosure)
// Data models
def boundedRangeModel(Map attributes = [:])
def spinnerDateModel(Map attributes = [:])
def spinnerListModel(Map attributes = [:])
def spinnerNumberModel(Map attributes = [:])Border factories for visual styling including line borders, bevel borders, etched borders, and compound borders.
// Border types
def lineBorder(Map attributes = [:])
def raisedBevelBorder(Map attributes = [:])
def loweredBevelBorder(Map attributes = [:])
def etchedBorder(Map attributes = [:])
def raisedEtchedBorder(Map attributes = [:])
def loweredEtchedBorder(Map attributes = [:])
def titledBorder(Map attributes = [:])
def emptyBorder(Map attributes = [:])
def compoundBorder(Map attributes = [:])
def matteBorder(Map attributes = [:])Menu component factories for creating menu bars, menus, menu items, and popup menus with action support.
// Menu components
def menuBar(Map attributes = [:], Closure closure = null)
def menu(Map attributes = [:], Closure closure = null)
def menuItem(Map attributes = [:], Closure closure = null)
def popupMenu(Map attributes = [:], Closure closure = null)
// Menu item types
def checkBoxMenuItem(Map attributes = [:], Closure closure = null)
def radioButtonMenuItem(Map attributes = [:], Closure closure = null)
// Actions
def action(Map attributes = [:])