0
# Groovy Swing
1
2
Groovy Swing provides a comprehensive domain-specific language (DSL) for building Java Swing user interfaces declaratively using Groovy's builder pattern. It eliminates the need for verbose imperative Java Swing code, offering factories for all major Swing components, data binding capabilities, and advanced features like custom renderers and layout managers.
3
4
## Package Information
5
6
- **Package Name**: groovy-swing
7
- **Package Type**: maven
8
- **Group ID**: org.apache.groovy
9
- **Language**: Groovy/Java
10
- **Installation**: Add to Gradle: `implementation 'org.apache.groovy:groovy-swing:5.0.0'`
11
12
## Core Imports
13
14
```groovy
15
import groovy.swing.SwingBuilder
16
import groovy.swing.LookAndFeelHelper
17
```
18
19
For Java integration:
20
```java
21
import groovy.swing.SwingBuilder;
22
import groovy.swing.LookAndFeelHelper;
23
```
24
25
## Basic Usage
26
27
```groovy
28
import groovy.swing.SwingBuilder
29
import javax.swing.*
30
31
def swing = new SwingBuilder()
32
33
def frame = swing.frame(title: 'Hello World', defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {
34
panel {
35
borderLayout()
36
label(text: 'Hello, Swing!', constraints: BorderLayout.CENTER)
37
button(text: 'Click Me', constraints: BorderLayout.SOUTH) {
38
actionPerformed {
39
JOptionPane.showMessageDialog(null, 'Button clicked!')
40
}
41
}
42
}
43
}
44
45
frame.pack()
46
frame.visible = true
47
```
48
49
## Architecture
50
51
Groovy Swing is built around several key components:
52
53
- **SwingBuilder**: Main DSL class that registers factories for all Swing components and provides builder pattern methods
54
- **Factory System**: 80+ factory classes that create and configure Swing components declaratively
55
- **Data Binding System**: ValueModel interfaces and binding classes for connecting UI components to data models
56
- **Threading Utilities**: EDT (Event Dispatch Thread) management methods for safe UI updates
57
- **Look and Feel Management**: LookAndFeelHelper for managing Swing look and feel settings
58
59
## Capabilities
60
61
### Core Builder API
62
63
The main SwingBuilder class and fundamental DSL capabilities for creating UI components, managing look and feel, and handling threading.
64
65
```groovy { .api }
66
class SwingBuilder extends FactoryBuilderSupport {
67
SwingBuilder(boolean init = true)
68
69
// Threading methods
70
def edt(Closure c)
71
def doLater(Closure c)
72
def doOutside(Closure c)
73
74
// Static factory methods
75
static def edtBuilder(Closure c)
76
static def lookAndFeel(Object laf, Closure initCode = null)
77
static def lookAndFeel(Map attributes = [:], Object laf = null, Closure initCode = null)
78
79
// Utility methods
80
def build(Closure c)
81
def shortcut(key, modifier = 0)
82
def createKeyStrokeAction(Map attributes, JComponent component = null)
83
}
84
85
class LookAndFeelHelper {
86
static LookAndFeelHelper getInstance()
87
static String getNimbusLAFName()
88
static String getAquaLAFName()
89
90
def addLookAndFeelAlias(String alias, String className)
91
def lookAndFeel(Object value, Map attributes, Closure initClosure)
92
}
93
```
94
95
[Core Builder API](./core-builder.md)
96
97
### UI Components
98
99
Factory-based DSL keywords for creating all major Swing UI components including windows, buttons, text fields, and containers.
100
101
```groovy { .api }
102
// Window components
103
def frame(Map attributes = [:], Closure closure = null)
104
def dialog(Map attributes = [:], Closure closure = null)
105
def window(Map attributes = [:], Closure closure = null)
106
def fileChooser(Map attributes = [:])
107
def optionPane(Map attributes = [:])
108
109
// Button components
110
def button(Map attributes = [:], Closure closure = null)
111
def checkBox(Map attributes = [:], Closure closure = null)
112
def radioButton(Map attributes = [:], Closure closure = null)
113
def toggleButton(Map attributes = [:], Closure closure = null)
114
115
// Text components
116
def textField(Map attributes = [:], Closure closure = null)
117
def textArea(Map attributes = [:], Closure closure = null)
118
def label(Map attributes = [:], Closure closure = null)
119
def passwordField(Map attributes = [:], Closure closure = null)
120
def formattedTextField(Map attributes = [:], Closure closure = null)
121
def editorPane(Map attributes = [:], Closure closure = null)
122
def textPane(Map attributes = [:], Closure closure = null)
123
124
// Container components
125
def panel(Map attributes = [:], Closure closure = null)
126
def scrollPane(Map attributes = [:], Closure closure = null)
127
def tabbedPane(Map attributes = [:], Closure closure = null)
128
def desktopPane(Map attributes = [:])
129
def layeredPane(Map attributes = [:])
130
def viewport(Map attributes = [:], Closure closure = null)
131
def toolBar(Map attributes = [:], Closure closure = null)
132
133
// Selection components
134
def list(Map attributes = [:], Closure closure = null)
135
def comboBox(Map attributes = [:], Closure closure = null)
136
def tree(Map attributes = [:])
137
def slider(Map attributes = [:])
138
def spinner(Map attributes = [:])
139
def colorChooser(Map attributes = [:])
140
def separator(Map attributes = [:])
141
def progressBar(Map attributes = [:])
142
def scrollBar(Map attributes = [:])
143
144
// Generic widget factories
145
def widget(Map attributes = [:], Closure closure = null)
146
def container(Map attributes = [:], Closure closure = null)
147
def bean(Map attributes = [:], Closure closure = null)
148
```
149
150
[UI Components](./components.md)
151
152
### Layout Management
153
154
Layout manager factories for organizing components including BorderLayout, GridLayout, BoxLayout, and advanced layouts like GridBagLayout and TableLayout.
155
156
```groovy { .api }
157
// Layout managers
158
def borderLayout(Map attributes = [:])
159
def flowLayout(Map attributes = [:])
160
def gridLayout(Map attributes = [:])
161
def gridBagLayout(Map attributes = [:])
162
def boxLayout(Map attributes = [:])
163
164
// Box layout components
165
def hbox(Map attributes = [:], Closure closure = null)
166
def vbox(Map attributes = [:], Closure closure = null)
167
def hglue()
168
def vglue()
169
def rigidArea(Map attributes = [:])
170
171
// Table layout
172
def tableLayout(Map attributes = [:])
173
def tr(Map attributes = [:], Closure closure = null)
174
def td(Map attributes = [:], Closure closure = null)
175
```
176
177
[Layout Management](./layouts.md)
178
179
### Support Utilities
180
181
Support utilities for actions, collections, resources, and keyboard shortcuts.
182
183
```groovy { .api }
184
// Actions and resources
185
def action(Map attributes = [:])
186
def actions(List actionList)
187
def imageIcon(Map attributes = [:])
188
def buttonGroup(Map attributes = [:])
189
190
// Collections and utilities
191
def map(Map attributes = [:])
192
def noparent(List items)
193
def actions(List actionList)
194
195
// Keyboard shortcuts
196
def keyStrokeAction(Map attributes, JComponent component = null)
197
```
198
199
[Support Utilities](./core-builder.md)
200
201
### Data Binding
202
203
Comprehensive data binding system with ValueModel interfaces, property binding, and automatic UI synchronization with data models.
204
205
```groovy { .api }
206
// Binding factories
207
def bind(Map attributes = [:])
208
def bindGroup(Map attributes = [:])
209
def bindProxy(Map attributes = [:])
210
211
// Data models
212
interface ValueModel {
213
Object getValue()
214
void setValue(Object value)
215
Class getType()
216
boolean isEditable()
217
}
218
219
class ValueHolder implements ValueModel {
220
ValueHolder()
221
ValueHolder(Object value)
222
ValueHolder(Object value, Class type)
223
}
224
225
class PropertyModel implements ValueModel {
226
PropertyModel(Object bean, String propertyName)
227
}
228
```
229
230
[Data Binding](./data-binding.md)
231
232
### Table Support
233
234
Enhanced table components with custom models, column definitions, renderers, and sorters for building data-driven table interfaces.
235
236
```groovy { .api }
237
// Table components
238
def table(Map attributes = [:], Closure closure = null)
239
def tableModel(Map attributes = [:], Closure closure = null)
240
def tableColumn(Map attributes = [:])
241
def columnModel(Map attributes = [:])
242
def column(Map attributes = [:])
243
244
// Column types
245
def propertyColumn(Map attributes = [:])
246
def closureColumn(Map attributes = [:])
247
248
// Renderers and editors
249
def tableCellRenderer(Map attributes = [:])
250
def listCellRenderer(Map attributes = [:])
251
def cellRenderer(Map attributes = [:])
252
def headerRenderer(Map attributes = [:])
253
def onRender(Closure renderClosure)
254
def cellEditor(Map attributes = [:])
255
def editorValue(Closure valueClosure)
256
def prepareEditor(Closure prepareClosure)
257
258
// Data models
259
def boundedRangeModel(Map attributes = [:])
260
def spinnerDateModel(Map attributes = [:])
261
def spinnerListModel(Map attributes = [:])
262
def spinnerNumberModel(Map attributes = [:])
263
```
264
265
[Table Support](./tables.md)
266
267
### Borders and Styling
268
269
Border factories for visual styling including line borders, bevel borders, etched borders, and compound borders.
270
271
```groovy { .api }
272
// Border types
273
def lineBorder(Map attributes = [:])
274
def raisedBevelBorder(Map attributes = [:])
275
def loweredBevelBorder(Map attributes = [:])
276
def etchedBorder(Map attributes = [:])
277
def raisedEtchedBorder(Map attributes = [:])
278
def loweredEtchedBorder(Map attributes = [:])
279
def titledBorder(Map attributes = [:])
280
def emptyBorder(Map attributes = [:])
281
def compoundBorder(Map attributes = [:])
282
def matteBorder(Map attributes = [:])
283
```
284
285
[Borders and Styling](./borders.md)
286
287
### Menu System
288
289
Menu component factories for creating menu bars, menus, menu items, and popup menus with action support.
290
291
```groovy { .api }
292
// Menu components
293
def menuBar(Map attributes = [:], Closure closure = null)
294
def menu(Map attributes = [:], Closure closure = null)
295
def menuItem(Map attributes = [:], Closure closure = null)
296
def popupMenu(Map attributes = [:], Closure closure = null)
297
298
// Menu item types
299
def checkBoxMenuItem(Map attributes = [:], Closure closure = null)
300
def radioButtonMenuItem(Map attributes = [:], Closure closure = null)
301
302
// Actions
303
def action(Map attributes = [:])
304
```
305
306
[Menu System](./menus.md)