0
# Core Builder API
1
2
The core builder API provides the fundamental SwingBuilder class and LookAndFeelHelper for creating Groovy Swing applications.
3
4
## SwingBuilder Class
5
6
The main entry point for building Swing UIs using Groovy's DSL syntax.
7
8
```groovy { .api }
9
class SwingBuilder extends FactoryBuilderSupport {
10
static final String DELEGATE_PROPERTY_OBJECT_ID = "_delegateProperty:id"
11
static final String DEFAULT_DELEGATE_PROPERTY_OBJECT_ID = "id"
12
13
SwingBuilder(boolean init = true)
14
}
15
```
16
17
### Static Factory Methods
18
19
```groovy { .api }
20
// Create SwingBuilder and execute closure in EDT
21
static def edtBuilder(Closure c)
22
23
// Look and feel configuration
24
static def lookAndFeel(Object laf, Closure initCode = null)
25
static def lookAndFeel(Map attributes = [:], Object laf = null, Closure initCode = null)
26
static def lookAndFeel(Object... lafs)
27
```
28
29
### Threading Methods
30
31
```groovy { .api }
32
// Execute closure in Event Dispatch Thread using SwingUtilities.invokeAndWait
33
def edt(Closure c)
34
35
// Execute closure in EDT using SwingUtilities.invokeLater
36
def doLater(Closure c)
37
38
// Execute closure outside EDT in separate thread
39
def doOutside(Closure c)
40
```
41
42
### Utility Methods
43
44
```groovy { .api }
45
// Build UI with closure
46
def build(Closure c)
47
48
// Create keyboard shortcuts
49
def shortcut(key, modifier = 0)
50
def shortcut(String key, modifier = 0)
51
52
// Create key stroke actions
53
def createKeyStrokeAction(Map attributes = [:], JComponent component = null)
54
55
// Explicit methods (registered via registerExplicitMethod)
56
def keyStrokeAction(Map attributes, JComponent component = null)
57
```
58
59
### Usage Examples
60
61
Basic SwingBuilder creation:
62
```groovy
63
def swing = new SwingBuilder()
64
65
// Create simple frame
66
def frame = swing.frame(title: 'My App') {
67
panel {
68
label(text: 'Hello World')
69
}
70
}
71
```
72
73
Using EDT builder:
74
```groovy
75
SwingBuilder.edtBuilder {
76
frame(title: 'EDT Frame', visible: true) {
77
label(text: 'Running in EDT')
78
}
79
}
80
```
81
82
Threading examples:
83
```groovy
84
def swing = new SwingBuilder()
85
86
// Execute in EDT
87
swing.edt {
88
// UI updates here
89
frame.title = 'Updated'
90
}
91
92
// Execute later in EDT
93
swing.doLater {
94
// Deferred UI updates
95
}
96
97
// Execute outside EDT
98
swing.doOutside {
99
// Background processing
100
def data = loadDataFromServer()
101
swing.edt {
102
// Update UI with results
103
updateTable(data)
104
}
105
}
106
```
107
108
## LookAndFeelHelper Class
109
110
Utility class for managing Swing Look and Feel settings.
111
112
```groovy { .api }
113
class LookAndFeelHelper {
114
// Singleton access
115
static LookAndFeelHelper getInstance()
116
117
// Predefined LAF names
118
static String getNimbusLAFName()
119
static String getAquaLAFName()
120
static String getSubstanceLAFName()
121
122
// Configuration methods
123
def addLookAndFeelAlias(String alias, String className)
124
def addLookAndFeelAttributeHandler(String className, String attr, Closure handler)
125
def lookAndFeel(Object value, Map attributes = [:], Closure initClosure = null)
126
127
// Tree node interface
128
boolean isLeaf()
129
}
130
```
131
132
### Predefined Look and Feel Aliases
133
134
```groovy { .api }
135
// Standard LAFs
136
"metal", "nimbus", "mac", "motif", "windows", "win2k", "gtk", "synth"
137
138
// System LAFs
139
"system", "crossPlatform"
140
141
// Third-party LAFs
142
"plastic", "plastic3D", "plasticXP" // JGoodies Plastic
143
"substance" // Substance LAF
144
"napkin" // Napkin LAF
145
```
146
147
### Usage Examples
148
149
Setting look and feel:
150
```groovy
151
import groovy.swing.SwingBuilder
152
153
// Using static method with alias
154
SwingBuilder.lookAndFeel('nimbus')
155
156
// Using static method with custom LAF
157
SwingBuilder.lookAndFeel('com.company.CustomLookAndFeel')
158
159
// With initialization code
160
SwingBuilder.lookAndFeel('nimbus') {
161
// LAF-specific configuration
162
}
163
164
// With attributes
165
SwingBuilder.lookAndFeel(
166
attributes: [someProperty: 'value'],
167
laf: 'nimbus'
168
) {
169
// Custom initialization
170
}
171
```
172
173
Using LookAndFeelHelper directly:
174
```groovy
175
def lafHelper = LookAndFeelHelper.getInstance()
176
177
// Add custom alias
178
lafHelper.addLookAndFeelAlias('myLAF', 'com.company.MyLookAndFeel')
179
180
// Set LAF with helper
181
lafHelper.lookAndFeel('myLAF')
182
```
183
184
## Support Nodes
185
186
Additional utility factories for creating support objects.
187
188
```groovy { .api }
189
// Action support
190
def action(Map attributes = [:])
191
def actions(List actionList)
192
193
// Collections
194
def map(Map attributes = [:])
195
def noparent(List items)
196
197
// Resources
198
def imageIcon(Map attributes = [:])
199
200
// Button grouping
201
def buttonGroup(Map attributes = [:])
202
```
203
204
### Action Examples
205
206
```groovy
207
def swing = new SwingBuilder()
208
209
// Define reusable action
210
def saveAction = swing.action(
211
name: 'Save',
212
closure: { println 'Saving...' },
213
mnemonic: 'S',
214
accelerator: swing.shortcut('S', KeyEvent.CTRL_MASK)
215
)
216
217
// Use action in multiple components
218
swing.frame(title: 'Action Demo') {
219
menuBar {
220
menu(text: 'File') {
221
menuItem(action: saveAction)
222
}
223
}
224
panel {
225
button(action: saveAction)
226
}
227
}
228
```
229
230
Button group example:
231
```groovy
232
swing.panel {
233
def group = buttonGroup()
234
radioButton(text: 'Option 1', buttonGroup: group)
235
radioButton(text: 'Option 2', buttonGroup: group)
236
radioButton(text: 'Option 3', buttonGroup: group)
237
}
238
```