CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-codehaus-groovy--groovy-swing

A declarative Swing GUI builder for Groovy applications that provides a concise and maintainable way to create complex Swing user interfaces

Pending
Overview
Eval results
Files

gui-building.mddocs/

GUI Building and Components

Core SwingBuilder functionality and factory system for declarative GUI construction using Groovy's builder pattern.

Capabilities

SwingBuilder Core

The primary builder class that orchestrates GUI construction through a comprehensive factory system.

/**
 * Primary declarative builder for creating Swing GUIs using Groovy's builder pattern
 */
class SwingBuilder extends FactoryBuilderSupport {
    /** Constructor with optional initialization of default factories */
    SwingBuilder(boolean init = true)
    
    /** Execute closure on Event Dispatch Thread using invokeAndWait */
    SwingBuilder edt(Closure c)
    
    /** Execute closure on EDT using invokeLater */
    SwingBuilder doLater(Closure c)
    
    /** Execute closure outside EDT in separate thread */
    SwingBuilder doOutside(Closure c)
    
    /** Compatibility method for building GUIs */
    Object build(Closure c)
    
    /** Create keyboard shortcuts with platform-specific modifiers */
    KeyStroke shortcut(key, modifier = 0)
    KeyStroke shortcut(String key, modifier = 0)
    
    /** Register key bindings for components */
    void createKeyStrokeAction(Map attributes, JComponent component = null)
    
    /** Factory method to create builder and run closure on EDT */
    static SwingBuilder edtBuilder(Closure c)
    
    /** Configure Look and Feel with various options */
    static LookAndFeel lookAndFeel(Object... lafs)
    static LookAndFeel lookAndFeel(Object laf, Closure initCode)
    static LookAndFeel lookAndFeel(Map attributes, Object laf, Closure initCode)
}

Usage Examples:

import groovy.swing.SwingBuilder
import javax.swing.*

// Basic builder creation and usage
def swing = new SwingBuilder()
def frame = swing.frame(title: 'My App') {
    panel {
        label(text: 'Hello World')
    }
}

// Thread-safe EDT building
SwingBuilder.edtBuilder {
    frame(title: 'EDT Safe', defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {
        button(text: 'Click Me')
    }.show()
}

// Key stroke actions
swing.createKeyStrokeAction(
    keyStroke: 'ctrl pressed N',
    action: { println 'New file action' },
    component: myPanel
)

// Platform shortcuts
def newShortcut = swing.shortcut(KeyEvent.VK_N) // Ctrl+N on Windows/Linux, Cmd+N on Mac

Window Components

Factory classes for creating top-level windows and dialogs.

// Frame creation with window management
frame(title: String, defaultCloseOperation: int = JFrame.HIDE_ON_CLOSE) { /* content */ }

// Dialog creation
dialog(title: String, modal: boolean = true, owner: Window = null) { /* content */ }

// Generic window
window(title: String, owner: Window = null) { /* content */ }

// Internal frames for MDI applications
internalFrame(title: String, closable: boolean = true, maximizable: boolean = true, 
              iconifiable: boolean = true, resizable: boolean = true) { /* content */ }

Usage Examples:

// Main application frame
frame(title: 'My Application', defaultCloseOperation: JFrame.EXIT_ON_CLOSE, 
      size: [800, 600]) {
    menuBar {
        menu(text: 'File') {
            menuItem(text: 'New', actionPerformed: { /* action */ })
            menuItem(text: 'Open', actionPerformed: { /* action */ })
        }
    }
    panel {
        // main content
    }
}

// Modal dialog
dialog(title: 'Settings', modal: true, size: [400, 300]) {
    panel {
        label(text: 'Configuration options:')
        // form fields
    }
}

Action Widgets

Components that can be associated with Action objects for consistent behavior.

// Buttons with action support
button(text: String, action: Action = null, actionPerformed: Closure = null)
checkBox(text: String, selected: boolean = false, action: Action = null)
radioButton(text: String, selected: boolean = false, action: Action = null)
toggleButton(text: String, selected: boolean = false, action: Action = null)

// Menu items with action support
menuItem(text: String, action: Action = null, actionPerformed: Closure = null)
checkBoxMenuItem(text: String, selected: boolean = false, action: Action = null)
radioButtonMenuItem(text: String, selected: boolean = false, action: Action = null)

Usage Examples:

// Button with closure action
button(text: 'Save', actionPerformed: { e ->
    // save logic here
    println 'Document saved'
})

// Shared action for multiple components
def exitAction = swing.action(
    name: 'Exit',
    shortDescription: 'Exit the application',
    accelerator: swing.shortcut('alt F4'),
    closure: { System.exit(0) }
)

menuItem(action: exitAction)
button(action: exitAction)

Text Components

Factory classes for text input and display components.

// Text input components
textField(text: String = '', columns: int = 0, editable: boolean = true)
passwordField(text: String = '', columns: int = 0, echoChar: char = '*')
textArea(text: String = '', rows: int = 0, columns: int = 0, lineWrap: boolean = false)
textPane(text: String = '', editable: boolean = true, contentType: String = 'text/plain')
editorPane(text: String = '', editable: boolean = true, contentType: String = 'text/plain')

// Formatted text field with input validation
formattedTextField(value: Object = null, format: Format = null, columns: int = 0)

// Display components
label(text: String = '', icon: Icon = null, horizontalAlignment: int = SwingConstants.LEFT)

Usage Examples:

// Text input form
panel {
    label(text: 'Name:')
    textField(id: 'nameField', columns: 20)
    
    label(text: 'Password:')
    passwordField(id: 'passwordField', columns: 20)
    
    label(text: 'Comments:')
    scrollPane {
        textArea(id: 'commentsArea', rows: 5, columns: 30, lineWrap: true)
    }
}

// Formatted number input
formattedTextField(
    value: 0.0,
    format: new DecimalFormat('#,##0.00'),
    columns: 10
)

Container Components

Components that can contain other components with various layout strategies.

// Basic containers
panel(layout: LayoutManager = new FlowLayout())
scrollPane(horizontalScrollBarPolicy: int = JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED,
           verticalScrollBarPolicy: int = JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED)

// Specialized containers
tabbedPane(tabPlacement: int = JTabbedPane.TOP)
splitPane(orientation: int = JSplitPane.HORIZONTAL_SPLIT, 
          continuousLayout: boolean = false, dividerLocation: int = -1)
toolBar(orientation: int = JToolBar.HORIZONTAL, floatable: boolean = true)

// Layered containers
layeredPane()
viewport()

Usage Examples:

// Tabbed interface
tabbedPane {
    panel(title: 'General') {
        // general settings
    }
    panel(title: 'Advanced') {
        // advanced settings
    }
}

// Split pane layout
splitPane(orientation: JSplitPane.HORIZONTAL_SPLIT, dividerLocation: 200) {
    scrollPane {
        tree(id: 'fileTree')
    }
    scrollPane {
        textArea(id: 'contentArea')
    }
}

Data Components

Components for displaying and editing structured data.

// List component with model support
list(listData: Object[] = null, model: ListModel = null, 
     selectionMode: int = ListSelectionModel.SINGLE_SELECTION)

// Combo box with data binding
comboBox(items: Object[] = null, model: ComboBoxModel = null, editable: boolean = false)

// Table with comprehensive model support
table(model: TableModel = null, columnModel: TableColumnModel = null,
      selectionModel: ListSelectionModel = null, autoCreateColumnsFromModel: boolean = true)

// Tree component
tree(model: TreeModel = null, rootVisible: boolean = true, showsRootHandles: boolean = false)

// Selection components
colorChooser(color: Color = Color.WHITE)
fileChooser(currentDirectory: File = null, fileSelectionMode: int = JFileChooser.FILES_ONLY)

Usage Examples:

// Data table with custom model
table {
    tableModel(list: myDataList) {
        propertyColumn(header: 'Name', propertyName: 'name')
        propertyColumn(header: 'Age', propertyName: 'age', type: Integer)
        closureColumn(header: 'Full Info') { row ->
            "${row.name} (${row.age})"
        }
    }
}

// Combo box with items
comboBox(items: ['Option 1', 'Option 2', 'Option 3'], 
         selectedIndex: 0,
         actionPerformed: { e ->
             println "Selected: ${e.source.selectedItem}"
         })

Utility Components

Various utility components for enhanced user interfaces.

// Value selection components
slider(minimum: int = 0, maximum: int = 100, value: int = 50, 
       orientation: int = JSlider.HORIZONTAL, majorTickSpacing: int = 0)
spinner(model: SpinnerModel = new SpinnerNumberModel(), value: Object = null)
progressBar(minimum: int = 0, maximum: int = 100, value: int = 0, 
            orientation: int = JProgressBar.HORIZONTAL, stringPainted: boolean = false)

// Separation components
separator(orientation: int = JSeparator.HORIZONTAL)
scrollBar(orientation: int = JScrollBar.VERTICAL, minimum: int = 0, maximum: int = 100)

// Special components
desktopPane()
optionPane(message: Object = null, messageType: int = JOptionPane.INFORMATION_MESSAGE)

Usage Examples:

// Settings panel with various controls
panel {
    label(text: 'Volume:')
    slider(minimum: 0, maximum: 100, value: 50, majorTickSpacing: 25,
           paintTicks: true, paintLabels: true)
    
    separator()
    
    label(text: 'Count:')
    spinner(model: new SpinnerNumberModel(1, 1, 100, 1))
}

Image and Icon Support

Factory for creating and managing icons and images.

// Image icon creation
imageIcon(url: URL = null, filename: String = null, resource: String = null,
          image: Image = null, description: String = null)

Usage Examples:

// Button with icon
button(text: 'Open File', 
       icon: imageIcon(resource: '/icons/open.png'),
       actionPerformed: { /* open file */ })

// Label with image
label(icon: imageIcon(filename: 'logo.png'), 
      horizontalAlignment: SwingConstants.CENTER)

Install with Tessl CLI

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

docs

data-binding.md

extensions.md

gui-building.md

index.md

layout-management.md

look-and-feel.md

models.md

tile.json