SwingBuilder for creating Swing GUIs in Groovy - provides DSL for declarative UI construction
—
Groovy Swing provides factory methods for creating all major Swing UI components through declarative DSL syntax.
Top-level window components for creating application windows.
// Main application windows
def frame(Map attributes = [:], Closure closure = null)
def window(Map attributes = [:], Closure closure = null)
// Dialog windows
def dialog(Map attributes = [:], Closure closure = null)
// File chooser dialog
def fileChooser(Map attributes = [:])
// Option pane dialogs
def optionPane(Map attributes = [:])
// Internal MDI windows
def internalFrame(Map attributes = [:], Closure closure = null)
def desktopPane(Map attributes = [:])// Main application frame
def frame = swing.frame(
title: 'My Application',
defaultCloseOperation: JFrame.EXIT_ON_CLOSE,
size: [800, 600]
) {
// Add components here
}
// Modal dialog
def dialog = swing.dialog(
owner: frame,
title: 'Settings',
modal: true,
size: [400, 300]
) {
// Dialog content
}
// File chooser
def chooser = swing.fileChooser(
dialogTitle: 'Select File',
fileSelectionMode: JFileChooser.FILES_ONLY
)Interactive button components with action support.
// Standard buttons
def button(Map attributes = [:], Closure closure = null)
def toggleButton(Map attributes = [:], Closure closure = null)
// Selection buttons
def checkBox(Map attributes = [:], Closure closure = null)
def radioButton(Map attributes = [:], Closure closure = null)
// Menu buttons
def menuItem(Map attributes = [:], Closure closure = null)
def checkBoxMenuItem(Map attributes = [:], Closure closure = null)
def radioButtonMenuItem(Map attributes = [:], Closure closure = null)// Push button with action
swing.button(
text: 'Click Me',
icon: imageIcon('/icons/button.png')
) {
actionPerformed { e ->
println 'Button clicked!'
}
}
// Checkbox with binding
swing.checkBox(
text: 'Enable feature',
selected: bind(source: model, sourceProperty: 'featureEnabled')
)
// Radio button group
def group = swing.buttonGroup()
swing.panel {
radioButton(text: 'Option 1', buttonGroup: group, selected: true)
radioButton(text: 'Option 2', buttonGroup: group)
radioButton(text: 'Option 3', buttonGroup: group)
}Text input and display components.
// Display components
def label(Map attributes = [:], Closure closure = null)
// Input components
def textField(Map attributes = [:], Closure closure = null)
def passwordField(Map attributes = [:], Closure closure = null)
def textArea(Map attributes = [:], Closure closure = null)
def formattedTextField(Map attributes = [:], Closure closure = null)
// Rich text components
def editorPane(Map attributes = [:], Closure closure = null)
def textPane(Map attributes = [:], Closure closure = null)// Label with styling
swing.label(
text: 'Username:',
font: new Font('Arial', Font.BOLD, 12),
foreground: Color.BLUE
)
// Text field with validation
swing.textField(
columns: 20,
text: bind(source: model, sourceProperty: 'username',
validator: { it?.length() > 0 })
)
// Text area with scrolling
swing.scrollPane {
textArea(
rows: 10,
columns: 40,
text: bind(source: model, sourceProperty: 'description')
)
}
// Formatted text field
swing.formattedTextField(
format: NumberFormat.getCurrencyInstance(),
value: bind(source: model, sourceProperty: 'price')
)Components for selecting from lists or ranges of values.
// List components
def list(Map attributes = [:], Closure closure = null)
def comboBox(Map attributes = [:], Closure closure = null)
// Tree component
def tree(Map attributes = [:])
// Range components
def slider(Map attributes = [:])
def spinner(Map attributes = [:])
def scrollBar(Map attributes = [:])
def progressBar(Map attributes = [:])
// Color selection
def colorChooser(Map attributes = [:])// List with custom model
swing.scrollPane {
list(
listData: ['Item 1', 'Item 2', 'Item 3'],
selectionMode: ListSelectionModel.SINGLE_SELECTION,
selectedValue: bind(source: model, sourceProperty: 'selectedItem')
)
}
// Combo box with binding
swing.comboBox(
items: bind(source: model, sourceProperty: 'availableOptions'),
selectedItem: bind(source: model, sourceProperty: 'selectedOption')
)
// Slider with value binding
swing.slider(
minimum: 0,
maximum: 100,
value: bind(source: model, sourceProperty: 'percentage')
)
// Spinner with number model
swing.spinner(
model: swing.spinnerNumberModel(
value: 50,
minimum: 0,
maximum: 100,
stepSize: 5
)
)Container components for organizing and grouping other components.
// Basic containers
def panel(Map attributes = [:], Closure closure = null)
def scrollPane(Map attributes = [:], Closure closure = null)
// Specialized containers
def tabbedPane(Map attributes = [:], Closure closure = null)
def splitPane(Map attributes = [:], Closure closure = null)
def toolBar(Map attributes = [:], Closure closure = null)
def layeredPane(Map attributes = [:])
def viewport(Map attributes = [:])// Panel with border layout
swing.panel {
borderLayout()
label(text: 'North', constraints: BorderLayout.NORTH)
label(text: 'Center', constraints: BorderLayout.CENTER)
label(text: 'South', constraints: BorderLayout.SOUTH)
}
// Tabbed pane
swing.tabbedPane {
panel(title: 'Tab 1') {
label(text: 'Content 1')
}
panel(title: 'Tab 2') {
label(text: 'Content 2')
}
}
// Split pane
swing.splitPane(
orientation: JSplitPane.HORIZONTAL_SPLIT,
dividerLocation: 200
) {
panel(constraints: 'left') {
label(text: 'Left side')
}
panel(constraints: 'right') {
label(text: 'Right side')
}
}
// Scroll pane with content
swing.scrollPane(
preferredSize: [300, 200],
horizontalScrollBarPolicy: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
) {
textArea(rows: 20, columns: 50)
}Visual separator components for organizing UI elements.
def separator(Map attributes = [:])swing.panel {
boxLayout(axis: BoxLayout.Y_AXIS)
label(text: 'Section 1')
separator()
label(text: 'Section 2')
separator(orientation: SwingConstants.VERTICAL)
label(text: 'Section 3')
}For creating arbitrary Swing components not covered by specific factories.
// Generic widget creation
def widget(Map attributes = [:], Closure closure = null)
def container(Map attributes = [:], Closure closure = null)
def bean(Map attributes = [:], Closure closure = null)// Create custom component
swing.widget(
component: new JCustomComponent(),
preferredSize: [200, 100]
) {
// Configure custom component
}
// Create any Swing component
swing.bean(
class: 'javax.swing.JSpinner',
value: 42
)Install with Tessl CLI
npx tessl i tessl/maven-org-apache-groovy--groovy-swing