0
# Swing Extensions
1
2
Groovy-specific enhancements to standard Swing components providing operator overloads and convenient methods for more idiomatic Groovy code.
3
4
## Capabilities
5
6
### Container Extensions
7
8
Enhanced methods for Swing containers providing collection-like operations.
9
10
```groovy { .api }
11
/**
12
* Collection-like operations for Container components
13
*/
14
class ContainerExtensions {
15
/** Get the number of components in the container */
16
static int size(Container self)
17
18
/** Get component at specified index */
19
static Component getAt(Container self, int index)
20
21
/** Add component using left-shift operator */
22
static Container leftShift(Container self, Component component)
23
24
/** Iterate over components */
25
static Iterator<Component> iterator(Container self)
26
27
/** Remove all components */
28
static void clear(Container self)
29
}
30
```
31
32
### ButtonGroup Extensions
33
34
Collection-like operations for ButtonGroup.
35
36
```groovy { .api }
37
/**
38
* Collection-like operations for ButtonGroup
39
*/
40
class ButtonGroupExtensions {
41
/** Get the number of buttons in the group */
42
static int size(ButtonGroup self)
43
44
/** Get button at specified index */
45
static AbstractButton getAt(ButtonGroup self, int index)
46
47
/** Add button using left-shift operator */
48
static ButtonGroup leftShift(ButtonGroup self, AbstractButton button)
49
50
/** Iterate over buttons */
51
static Iterator<AbstractButton> iterator(ButtonGroup self)
52
}
53
```
54
55
### List Model Extensions
56
57
Enhanced methods for list models and combo boxes.
58
59
```groovy { .api }
60
/**
61
* Collection-like operations for ListModel
62
*/
63
class ListModelExtensions {
64
/** Get the size of the list model */
65
static int size(ListModel self)
66
67
/** Get element at specified index */
68
static Object getAt(ListModel self, int index)
69
70
/** Iterate over elements */
71
static Iterator iterator(ListModel self)
72
}
73
74
/**
75
* Mutable operations for DefaultListModel
76
*/
77
class DefaultListModelExtensions {
78
/** Add element using left-shift operator */
79
static DefaultListModel leftShift(DefaultListModel self, Object element)
80
81
/** Set element at specified index */
82
static void putAt(DefaultListModel self, int index, Object element)
83
84
/** Remove all elements */
85
static void clear(DefaultListModel self)
86
}
87
```
88
89
### ComboBox Extensions
90
91
Collection-like operations for JComboBox.
92
93
```groovy { .api }
94
/**
95
* Collection-like operations for JComboBox
96
*/
97
class JComboBoxExtensions {
98
/** Get the number of items */
99
static int size(JComboBox self)
100
101
/** Get item at specified index */
102
static Object getAt(JComboBox self, int index)
103
104
/** Add item using left-shift operator */
105
static JComboBox leftShift(JComboBox self, Object item)
106
107
/** Remove all items */
108
static void clear(JComboBox self)
109
110
/** Iterate over items */
111
static Iterator iterator(JComboBox self)
112
}
113
```
114
115
### Table Model Extensions
116
117
Enhanced methods for table models and operations.
118
119
```groovy { .api }
120
/**
121
* Collection-like operations for TableModel
122
*/
123
class TableModelExtensions {
124
/** Get the number of rows */
125
static int size(TableModel self)
126
127
/** Get row at specified index as array */
128
static Object[] getAt(TableModel self, int index)
129
130
/** Iterate over rows */
131
static Iterator iterator(TableModel self)
132
}
133
134
/**
135
* Mutable operations for DefaultTableModel
136
*/
137
class DefaultTableModelExtensions {
138
/** Add row using left-shift operator */
139
static DefaultTableModel leftShift(DefaultTableModel self, Object[] row)
140
141
/** Add row using left-shift operator */
142
static DefaultTableModel leftShift(DefaultTableModel self, Vector row)
143
144
/** Iterate over rows */
145
static Iterator iterator(DefaultTableModel self)
146
}
147
```
148
149
### Tree Extensions
150
151
Enhanced methods for tree components and models.
152
153
```groovy { .api }
154
/**
155
* Collection-like operations for TreePath
156
*/
157
class TreePathExtensions {
158
/** Get the path length */
159
static int size(TreePath self)
160
161
/** Get path component at specified index */
162
static Object getAt(TreePath self, int index)
163
164
/** Append object to path using left-shift operator */
165
static TreePath leftShift(TreePath self, Object object)
166
167
/** Iterate over path components */
168
static Iterator iterator(TreePath self)
169
}
170
171
/**
172
* Collection-like operations for TreeNode
173
*/
174
class TreeNodeExtensions {
175
/** Get the number of children */
176
static int size(TreeNode self)
177
178
/** Get child at specified index */
179
static TreeNode getAt(TreeNode self, int index)
180
181
/** Iterate over children */
182
static Iterator<TreeNode> iterator(TreeNode self)
183
}
184
185
/**
186
* Mutable operations for MutableTreeNode
187
*/
188
class MutableTreeNodeExtensions {
189
/** Add child using left-shift operator */
190
static MutableTreeNode leftShift(MutableTreeNode self, MutableTreeNode node)
191
192
/** Set child at specified index */
193
static void putAt(MutableTreeNode self, int index, MutableTreeNode node)
194
}
195
```
196
197
### Menu Extensions
198
199
Collection-like operations for JMenu components.
200
201
```groovy { .api }
202
/**
203
* Collection-like operations for JMenu
204
*/
205
class JMenuExtensions {
206
/** Get the number of menu items */
207
static int size(JMenu self)
208
209
/** Get menu item at specified index */
210
static Component getAt(JMenu self, int index)
211
212
/** Add menu item using left-shift operator */
213
static JMenu leftShift(JMenu self, JMenuItem item)
214
215
/** Add menu using left-shift operator */
216
static JMenu leftShift(JMenu self, JMenu menu)
217
218
/** Add component using left-shift operator */
219
static JMenu leftShift(JMenu self, Component component)
220
221
/** Add action using left-shift operator */
222
static JMenu leftShift(JMenu self, Action action)
223
224
/** Iterate over menu components */
225
static Iterator<Component> iterator(JMenu self)
226
}
227
```
228
229
### TabbedPane Extensions
230
231
Enhanced methods for JTabbedPane.
232
233
```groovy { .api }
234
/**
235
* Collection-like operations for JTabbedPane
236
*/
237
class JTabbedPaneExtensions {
238
/** Get the number of tabs */
239
static int size(JTabbedPane self)
240
241
/** Get tab component at specified index */
242
static Component getAt(JTabbedPane self, int index)
243
244
/** Remove all tabs */
245
static void clear(JTabbedPane self)
246
247
/** Iterate over tab components */
248
static Iterator<Component> iterator(JTabbedPane self)
249
}
250
```
251
252
### Button Extensions
253
254
Enhanced mnemonic support for buttons.
255
256
```groovy { .api }
257
/**
258
* Enhanced button operations
259
*/
260
class AbstractButtonExtensions {
261
/** Set mnemonic from single character string */
262
static void setMnemonic(AbstractButton self, String mnemonic)
263
}
264
```
265
266
## Usage Examples
267
268
### Container Operations
269
270
```groovy
271
import groovy.swing.SwingBuilder
272
import javax.swing.*
273
274
def swing = new SwingBuilder()
275
def panel = swing.panel()
276
277
// Use collection-like operations
278
panel << swing.button(text: 'Button 1')
279
panel << swing.button(text: 'Button 2')
280
panel << swing.button(text: 'Button 3')
281
282
println "Panel has ${panel.size()} components"
283
284
// Iterate over components
285
panel.each { component ->
286
println "Component: ${component.class.simpleName}"
287
}
288
289
// Access by index
290
def firstButton = panel[0]
291
```
292
293
### List Model Operations
294
295
```groovy
296
def listModel = new DefaultListModel()
297
298
// Add items using left-shift
299
listModel << 'Item 1'
300
listModel << 'Item 2'
301
listModel << 'Item 3'
302
303
// Access and modify
304
listModel[1] = 'Modified Item 2'
305
306
// Iterate
307
listModel.each { item ->
308
println "Item: $item"
309
}
310
311
println "List has ${listModel.size()} items"
312
```
313
314
### ComboBox Operations
315
316
```groovy
317
def comboBox = swing.comboBox()
318
319
// Add items using left-shift
320
comboBox << 'Option A'
321
comboBox << 'Option B'
322
comboBox << 'Option C'
323
324
// Access by index
325
def firstOption = comboBox[0]
326
327
// Clear all items
328
comboBox.clear()
329
```
330
331
### Button Mnemonic
332
333
```groovy
334
def button = swing.button(text: 'Save File')
335
336
// Set mnemonic using single character
337
button.mnemonic = 'S' // Equivalent to button.setMnemonic('S')
338
```