0
# UI Components
1
2
Groovy Swing provides factory methods for creating all major Swing UI components through declarative DSL syntax.
3
4
## Window Components
5
6
Top-level window components for creating application windows.
7
8
```groovy { .api }
9
// Main application windows
10
def frame(Map attributes = [:], Closure closure = null)
11
def window(Map attributes = [:], Closure closure = null)
12
13
// Dialog windows
14
def dialog(Map attributes = [:], Closure closure = null)
15
16
// File chooser dialog
17
def fileChooser(Map attributes = [:])
18
19
// Option pane dialogs
20
def optionPane(Map attributes = [:])
21
22
// Internal MDI windows
23
def internalFrame(Map attributes = [:], Closure closure = null)
24
def desktopPane(Map attributes = [:])
25
```
26
27
### Window Examples
28
29
```groovy
30
// Main application frame
31
def frame = swing.frame(
32
title: 'My Application',
33
defaultCloseOperation: JFrame.EXIT_ON_CLOSE,
34
size: [800, 600]
35
) {
36
// Add components here
37
}
38
39
// Modal dialog
40
def dialog = swing.dialog(
41
owner: frame,
42
title: 'Settings',
43
modal: true,
44
size: [400, 300]
45
) {
46
// Dialog content
47
}
48
49
// File chooser
50
def chooser = swing.fileChooser(
51
dialogTitle: 'Select File',
52
fileSelectionMode: JFileChooser.FILES_ONLY
53
)
54
```
55
56
## Button Components
57
58
Interactive button components with action support.
59
60
```groovy { .api }
61
// Standard buttons
62
def button(Map attributes = [:], Closure closure = null)
63
def toggleButton(Map attributes = [:], Closure closure = null)
64
65
// Selection buttons
66
def checkBox(Map attributes = [:], Closure closure = null)
67
def radioButton(Map attributes = [:], Closure closure = null)
68
69
// Menu buttons
70
def menuItem(Map attributes = [:], Closure closure = null)
71
def checkBoxMenuItem(Map attributes = [:], Closure closure = null)
72
def radioButtonMenuItem(Map attributes = [:], Closure closure = null)
73
```
74
75
### Button Examples
76
77
```groovy
78
// Push button with action
79
swing.button(
80
text: 'Click Me',
81
icon: imageIcon('/icons/button.png')
82
) {
83
actionPerformed { e ->
84
println 'Button clicked!'
85
}
86
}
87
88
// Checkbox with binding
89
swing.checkBox(
90
text: 'Enable feature',
91
selected: bind(source: model, sourceProperty: 'featureEnabled')
92
)
93
94
// Radio button group
95
def group = swing.buttonGroup()
96
swing.panel {
97
radioButton(text: 'Option 1', buttonGroup: group, selected: true)
98
radioButton(text: 'Option 2', buttonGroup: group)
99
radioButton(text: 'Option 3', buttonGroup: group)
100
}
101
```
102
103
## Text Components
104
105
Text input and display components.
106
107
```groovy { .api }
108
// Display components
109
def label(Map attributes = [:], Closure closure = null)
110
111
// Input components
112
def textField(Map attributes = [:], Closure closure = null)
113
def passwordField(Map attributes = [:], Closure closure = null)
114
def textArea(Map attributes = [:], Closure closure = null)
115
def formattedTextField(Map attributes = [:], Closure closure = null)
116
117
// Rich text components
118
def editorPane(Map attributes = [:], Closure closure = null)
119
def textPane(Map attributes = [:], Closure closure = null)
120
```
121
122
### Text Component Examples
123
124
```groovy
125
// Label with styling
126
swing.label(
127
text: 'Username:',
128
font: new Font('Arial', Font.BOLD, 12),
129
foreground: Color.BLUE
130
)
131
132
// Text field with validation
133
swing.textField(
134
columns: 20,
135
text: bind(source: model, sourceProperty: 'username',
136
validator: { it?.length() > 0 })
137
)
138
139
// Text area with scrolling
140
swing.scrollPane {
141
textArea(
142
rows: 10,
143
columns: 40,
144
text: bind(source: model, sourceProperty: 'description')
145
)
146
}
147
148
// Formatted text field
149
swing.formattedTextField(
150
format: NumberFormat.getCurrencyInstance(),
151
value: bind(source: model, sourceProperty: 'price')
152
)
153
```
154
155
## Selection Components
156
157
Components for selecting from lists or ranges of values.
158
159
```groovy { .api }
160
// List components
161
def list(Map attributes = [:], Closure closure = null)
162
def comboBox(Map attributes = [:], Closure closure = null)
163
164
// Tree component
165
def tree(Map attributes = [:])
166
167
// Range components
168
def slider(Map attributes = [:])
169
def spinner(Map attributes = [:])
170
def scrollBar(Map attributes = [:])
171
def progressBar(Map attributes = [:])
172
173
// Color selection
174
def colorChooser(Map attributes = [:])
175
```
176
177
### Selection Component Examples
178
179
```groovy
180
// List with custom model
181
swing.scrollPane {
182
list(
183
listData: ['Item 1', 'Item 2', 'Item 3'],
184
selectionMode: ListSelectionModel.SINGLE_SELECTION,
185
selectedValue: bind(source: model, sourceProperty: 'selectedItem')
186
)
187
}
188
189
// Combo box with binding
190
swing.comboBox(
191
items: bind(source: model, sourceProperty: 'availableOptions'),
192
selectedItem: bind(source: model, sourceProperty: 'selectedOption')
193
)
194
195
// Slider with value binding
196
swing.slider(
197
minimum: 0,
198
maximum: 100,
199
value: bind(source: model, sourceProperty: 'percentage')
200
)
201
202
// Spinner with number model
203
swing.spinner(
204
model: swing.spinnerNumberModel(
205
value: 50,
206
minimum: 0,
207
maximum: 100,
208
stepSize: 5
209
)
210
)
211
```
212
213
## Container Components
214
215
Container components for organizing and grouping other components.
216
217
```groovy { .api }
218
// Basic containers
219
def panel(Map attributes = [:], Closure closure = null)
220
def scrollPane(Map attributes = [:], Closure closure = null)
221
222
// Specialized containers
223
def tabbedPane(Map attributes = [:], Closure closure = null)
224
def splitPane(Map attributes = [:], Closure closure = null)
225
def toolBar(Map attributes = [:], Closure closure = null)
226
def layeredPane(Map attributes = [:])
227
def viewport(Map attributes = [:])
228
```
229
230
### Container Examples
231
232
```groovy
233
// Panel with border layout
234
swing.panel {
235
borderLayout()
236
label(text: 'North', constraints: BorderLayout.NORTH)
237
label(text: 'Center', constraints: BorderLayout.CENTER)
238
label(text: 'South', constraints: BorderLayout.SOUTH)
239
}
240
241
// Tabbed pane
242
swing.tabbedPane {
243
panel(title: 'Tab 1') {
244
label(text: 'Content 1')
245
}
246
panel(title: 'Tab 2') {
247
label(text: 'Content 2')
248
}
249
}
250
251
// Split pane
252
swing.splitPane(
253
orientation: JSplitPane.HORIZONTAL_SPLIT,
254
dividerLocation: 200
255
) {
256
panel(constraints: 'left') {
257
label(text: 'Left side')
258
}
259
panel(constraints: 'right') {
260
label(text: 'Right side')
261
}
262
}
263
264
// Scroll pane with content
265
swing.scrollPane(
266
preferredSize: [300, 200],
267
horizontalScrollBarPolicy: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
268
) {
269
textArea(rows: 20, columns: 50)
270
}
271
```
272
273
## Separator Components
274
275
Visual separator components for organizing UI elements.
276
277
```groovy { .api }
278
def separator(Map attributes = [:])
279
```
280
281
### Separator Examples
282
283
```groovy
284
swing.panel {
285
boxLayout(axis: BoxLayout.Y_AXIS)
286
label(text: 'Section 1')
287
separator()
288
label(text: 'Section 2')
289
separator(orientation: SwingConstants.VERTICAL)
290
label(text: 'Section 3')
291
}
292
```
293
294
## Generic Widget Factory
295
296
For creating arbitrary Swing components not covered by specific factories.
297
298
```groovy { .api }
299
// Generic widget creation
300
def widget(Map attributes = [:], Closure closure = null)
301
def container(Map attributes = [:], Closure closure = null)
302
def bean(Map attributes = [:], Closure closure = null)
303
```
304
305
### Generic Widget Examples
306
307
```groovy
308
// Create custom component
309
swing.widget(
310
component: new JCustomComponent(),
311
preferredSize: [200, 100]
312
) {
313
// Configure custom component
314
}
315
316
// Create any Swing component
317
swing.bean(
318
class: 'javax.swing.JSpinner',
319
value: 42
320
)
321
```