SwingBuilder for creating Swing GUIs in Groovy - provides DSL for declarative UI construction
—
Groovy Swing provides factory methods for all major Swing layout managers, enabling declarative layout configuration.
Core Swing layout managers for organizing components.
// Basic layouts
def borderLayout(Map attributes = [:])
def flowLayout(Map attributes = [:])
def gridLayout(Map attributes = [:])
def cardLayout(Map attributes = [:])
// Advanced layouts
def gridBagLayout(Map attributes = [:])
def springLayout(Map attributes = [:])
def overlayLayout(Map attributes = [:])
// GridBag constraints
def gridBagConstraints(Map attributes = [:])
def gbc(Map attributes = [:]) // Shortcut for gridBagConstraints// Border layout
swing.panel {
borderLayout(hgap: 5, vgap: 5)
button(text: 'North', constraints: BorderLayout.NORTH)
button(text: 'South', constraints: BorderLayout.SOUTH)
button(text: 'East', constraints: BorderLayout.EAST)
button(text: 'West', constraints: BorderLayout.WEST)
button(text: 'Center', constraints: BorderLayout.CENTER)
}
// Grid layout
swing.panel {
gridLayout(rows: 2, columns: 3, hgap: 5, vgap: 5)
(1..6).each { i ->
button(text: "Button $i")
}
}
// Flow layout
swing.panel {
flowLayout(alignment: FlowLayout.CENTER, hgap: 10)
button(text: 'Left')
button(text: 'Center')
button(text: 'Right')
}
// Card layout with switching
swing.panel {
cardLayout()
panel(constraints: 'card1') {
label(text: 'Card 1 Content')
}
panel(constraints: 'card2') {
label(text: 'Card 2 Content')
}
}GridBagLayout with constraint support for complex layouts.
swing.panel {
gridBagLayout()
label(text: 'Name:', constraints: gbc(gridx: 0, gridy: 0, anchor: GridBagConstraints.EAST))
textField(constraints: gbc(gridx: 1, gridy: 0, fill: GridBagConstraints.HORIZONTAL, weightx: 1.0))
label(text: 'Email:', constraints: gbc(gridx: 0, gridy: 1, anchor: GridBagConstraints.EAST))
textField(constraints: gbc(gridx: 1, gridy: 1, fill: GridBagConstraints.HORIZONTAL, weightx: 1.0))
button(text: 'Submit', constraints: gbc(gridx: 1, gridy: 2, anchor: GridBagConstraints.EAST))
}Box layout and related components for linear arrangements.
// Box layout manager
def boxLayout(Map attributes = [:])
// Box containers
def box(Map attributes = [:], Closure closure = null)
def hbox(Map attributes = [:], Closure closure = null) // Horizontal box
def vbox(Map attributes = [:], Closure closure = null) // Vertical box
// Spacing components
def glue(Map attributes = [:])
def hglue(Map attributes = [:]) // Horizontal glue
def vglue(Map attributes = [:]) // Vertical glue
def hstrut(Map attributes = [:]) // Horizontal strut
def vstrut(Map attributes = [:]) // Vertical strut
def rigidArea(Map attributes = [:])// Horizontal box with spacing
swing.hbox {
button(text: 'Left')
hglue() // Push remaining items to right
button(text: 'Center')
hglue()
button(text: 'Right')
}
// Vertical box with struts
swing.vbox {
button(text: 'Top')
vstrut(height: 10) // Fixed spacing
button(text: 'Middle')
vglue() // Variable spacing
button(text: 'Bottom')
}
// Custom box layout
swing.panel {
boxLayout(axis: BoxLayout.Y_AXIS)
button(text: 'Button 1')
rigidArea(width: 5, height: 5) // Fixed spacer
button(text: 'Button 2')
}
// Nested boxes for complex layouts
swing.vbox {
hbox {
button(text: '1')
button(text: '2')
hglue()
button(text: '3')
}
vstrut(height: 10)
hbox {
hglue()
button(text: '4')
button(text: '5')
}
}Custom table-based layout manager with row and cell support.
// Table layout manager
def tableLayout(Map attributes = [:])
// Table structure
def tr(Map attributes = [:], Closure closure = null) // Table row
def td(Map attributes = [:], Closure closure = null) // Table cellswing.panel {
tableLayout {
tr {
td { label(text: 'Name:') }
td { textField(columns: 20) }
}
tr {
td { label(text: 'Email:') }
td { textField(columns: 20) }
}
tr {
td(colspan: 2) {
button(text: 'Submit')
}
}
}
}
// Table layout with spacing and alignment
swing.panel {
tableLayout(
columnSpacing: 5,
rowSpacing: 5
) {
tr(alignment: 'center') {
td(width: 100) { label(text: 'Label 1') }
td(width: 200) { textField() }
td { button(text: 'Browse') }
}
tr {
td { label(text: 'Label 2') }
td(colspan: 2) { textArea(rows: 3) }
}
}
}Layout constraint handling for various layout managers.
// Constraint delegation - automatically handled by layout factories
// Components can specify constraints via 'constraints' attribute// Border layout constraints
swing.panel {
borderLayout()
button(text: 'North', constraints: BorderLayout.NORTH)
button(text: 'Center', constraints: BorderLayout.CENTER)
}
// GridBag constraints with direct attributes
swing.panel {
gridBagLayout()
button(
text: 'Stretch Button',
constraints: gbc(
gridx: 0, gridy: 0,
gridwidth: 2, gridheight: 1,
weightx: 1.0, weighty: 0.0,
fill: GridBagConstraints.HORIZONTAL,
anchor: GridBagConstraints.NORTHWEST,
insets: new Insets(5, 5, 5, 5)
)
)
}
// Split pane constraints
swing.splitPane {
panel(constraints: 'left') { /* left content */ }
panel(constraints: 'right') { /* right content */ }
}
// Tabbed pane constraints (title and optional icon)
swing.tabbedPane {
panel(title: 'Tab 1', icon: myIcon) { /* tab content */ }
panel(title: 'Tab 2') { /* tab content */ }
}The layout factory system provides automatic constraint delegation.
// Layout factories automatically handle:
// - Layout manager creation and configuration
// - Constraint attribute delegation
// - Component constraint application
// - Parent-child relationships// Combining layouts for complex UIs
swing.frame(title: 'Complex Layout') {
borderLayout()
// Top toolbar
toolBar(constraints: BorderLayout.NORTH) {
button(text: 'New')
button(text: 'Open')
separator()
button(text: 'Save')
}
// Main content area
splitPane(constraints: BorderLayout.CENTER, orientation: JSplitPane.HORIZONTAL_SPLIT) {
// Left sidebar
panel(constraints: 'left') {
borderLayout()
label(text: 'Sidebar', constraints: BorderLayout.NORTH)
scrollPane(constraints: BorderLayout.CENTER) {
tree()
}
}
// Right content
tabbedPane(constraints: 'right') {
panel(title: 'Document 1') {
borderLayout()
scrollPane(constraints: BorderLayout.CENTER) {
textArea()
}
}
panel(title: 'Document 2') {
vbox {
hbox {
label(text: 'Form Field:')
textField()
}
vstrut(height: 10)
scrollPane {
table()
}
}
}
}
}
// Status bar
label(text: 'Ready', constraints: BorderLayout.SOUTH)
}Install with Tessl CLI
npx tessl i tessl/maven-org-apache-groovy--groovy-swing