CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-groovy--groovy-swing

SwingBuilder for creating Swing GUIs in Groovy - provides DSL for declarative UI construction

Pending
Overview
Eval results
Files

core-builder.mddocs/

Core Builder API

The core builder API provides the fundamental SwingBuilder class and LookAndFeelHelper for creating Groovy Swing applications.

SwingBuilder Class

The main entry point for building Swing UIs using Groovy's DSL syntax.

class SwingBuilder extends FactoryBuilderSupport {
    static final String DELEGATE_PROPERTY_OBJECT_ID = "_delegateProperty:id"
    static final String DEFAULT_DELEGATE_PROPERTY_OBJECT_ID = "id"
    
    SwingBuilder(boolean init = true)
}

Static Factory Methods

// Create SwingBuilder and execute closure in EDT
static def edtBuilder(Closure c)

// Look and feel configuration
static def lookAndFeel(Object laf, Closure initCode = null)
static def lookAndFeel(Map attributes = [:], Object laf = null, Closure initCode = null)
static def lookAndFeel(Object... lafs)

Threading Methods

// Execute closure in Event Dispatch Thread using SwingUtilities.invokeAndWait
def edt(Closure c)

// Execute closure in EDT using SwingUtilities.invokeLater
def doLater(Closure c)

// Execute closure outside EDT in separate thread
def doOutside(Closure c)

Utility Methods

// Build UI with closure
def build(Closure c)

// Create keyboard shortcuts
def shortcut(key, modifier = 0)
def shortcut(String key, modifier = 0)

// Create key stroke actions
def createKeyStrokeAction(Map attributes = [:], JComponent component = null)

// Explicit methods (registered via registerExplicitMethod)
def keyStrokeAction(Map attributes, JComponent component = null)

Usage Examples

Basic SwingBuilder creation:

def swing = new SwingBuilder()

// Create simple frame
def frame = swing.frame(title: 'My App') {
    panel {
        label(text: 'Hello World')
    }
}

Using EDT builder:

SwingBuilder.edtBuilder {
    frame(title: 'EDT Frame', visible: true) {
        label(text: 'Running in EDT')
    }
}

Threading examples:

def swing = new SwingBuilder()

// Execute in EDT
swing.edt {
    // UI updates here
    frame.title = 'Updated'
}

// Execute later in EDT
swing.doLater {
    // Deferred UI updates
}

// Execute outside EDT
swing.doOutside {
    // Background processing
    def data = loadDataFromServer()
    swing.edt {
        // Update UI with results
        updateTable(data)
    }
}

LookAndFeelHelper Class

Utility class for managing Swing Look and Feel settings.

class LookAndFeelHelper {
    // Singleton access
    static LookAndFeelHelper getInstance()
    
    // Predefined LAF names
    static String getNimbusLAFName()
    static String getAquaLAFName() 
    static String getSubstanceLAFName()
    
    // Configuration methods
    def addLookAndFeelAlias(String alias, String className)
    def addLookAndFeelAttributeHandler(String className, String attr, Closure handler)
    def lookAndFeel(Object value, Map attributes = [:], Closure initClosure = null)
    
    // Tree node interface
    boolean isLeaf()
}

Predefined Look and Feel Aliases

// Standard LAFs
"metal", "nimbus", "mac", "motif", "windows", "win2k", "gtk", "synth"

// System LAFs
"system", "crossPlatform"

// Third-party LAFs
"plastic", "plastic3D", "plasticXP"  // JGoodies Plastic
"substance"                          // Substance LAF
"napkin"                            // Napkin LAF

Usage Examples

Setting look and feel:

import groovy.swing.SwingBuilder

// Using static method with alias
SwingBuilder.lookAndFeel('nimbus')

// Using static method with custom LAF
SwingBuilder.lookAndFeel('com.company.CustomLookAndFeel')

// With initialization code
SwingBuilder.lookAndFeel('nimbus') {
    // LAF-specific configuration
}

// With attributes
SwingBuilder.lookAndFeel(
    attributes: [someProperty: 'value'],
    laf: 'nimbus'
) {
    // Custom initialization
}

Using LookAndFeelHelper directly:

def lafHelper = LookAndFeelHelper.getInstance()

// Add custom alias
lafHelper.addLookAndFeelAlias('myLAF', 'com.company.MyLookAndFeel')

// Set LAF with helper
lafHelper.lookAndFeel('myLAF')

Support Nodes

Additional utility factories for creating support objects.

// Action support
def action(Map attributes = [:])
def actions(List actionList)

// Collections
def map(Map attributes = [:])
def noparent(List items)

// Resources
def imageIcon(Map attributes = [:])

// Button grouping
def buttonGroup(Map attributes = [:])

Action Examples

def swing = new SwingBuilder()

// Define reusable action
def saveAction = swing.action(
    name: 'Save',
    closure: { println 'Saving...' },
    mnemonic: 'S',
    accelerator: swing.shortcut('S', KeyEvent.CTRL_MASK)
)

// Use action in multiple components
swing.frame(title: 'Action Demo') {
    menuBar {
        menu(text: 'File') {
            menuItem(action: saveAction)
        }
    }
    panel {
        button(action: saveAction)
    }
}

Button group example:

swing.panel {
    def group = buttonGroup()
    radioButton(text: 'Option 1', buttonGroup: group)
    radioButton(text: 'Option 2', buttonGroup: group) 
    radioButton(text: 'Option 3', buttonGroup: group)
}

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-groovy--groovy-swing

docs

borders.md

components.md

core-builder.md

data-binding.md

index.md

layouts.md

menus.md

tables.md

tile.json