0
# Layout Management
1
2
Comprehensive layout system including all standard Swing layouts plus custom table-based layout manager with enhanced constraint handling.
3
4
## Capabilities
5
6
### Standard Layout Managers
7
8
Enhanced factories for all standard Swing layout managers with simplified attribute handling.
9
10
```groovy { .api }
11
/**
12
* Generic layout manager factory
13
*/
14
class LayoutFactory {
15
LayoutFactory(Class layoutClass)
16
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
17
static void constraintsAttributeDelegate(FactoryBuilderSupport builder, Object node, Map attributes)
18
}
19
20
// Border layout with gap support
21
borderLayout(hgap: int = 0, vgap: int = 0)
22
// Components can use constraints: BorderLayout.NORTH, SOUTH, EAST, WEST, CENTER
23
component(constraints: BorderLayout.NORTH)
24
25
// Grid layout with rows and columns
26
gridLayout(rows: int = 1, cols: int = 0, hgap: int = 0, vgap: int = 0)
27
28
// Flow layout with alignment options
29
flowLayout(alignment: int = FlowLayout.CENTER, hgap: int = 5, vgap: int = 5)
30
31
// Card layout for switching between components
32
cardLayout(hgap: int = 0, vgap: int = 0)
33
34
// Overlay layout for layered components
35
overlayLayout()
36
37
// Spring layout for precise positioning
38
springLayout()
39
```
40
41
**Usage Examples:**
42
43
```groovy
44
// Border layout with positioned components
45
panel {
46
borderLayout()
47
label(text: 'Header', constraints: BorderLayout.NORTH)
48
panel(constraints: BorderLayout.CENTER) {
49
// main content
50
}
51
button(text: 'OK', constraints: BorderLayout.SOUTH)
52
}
53
54
// Grid layout for uniform component sizing
55
panel {
56
gridLayout(rows: 2, cols: 3, hgap: 5, vgap: 5)
57
(1..6).each { i ->
58
button(text: "Button $i")
59
}
60
}
61
62
// Flow layout with custom alignment
63
panel {
64
flowLayout(alignment: FlowLayout.RIGHT, hgap: 10)
65
button(text: 'Cancel')
66
button(text: 'OK')
67
}
68
```
69
70
### GridBag Layout
71
72
Enhanced GridBagLayout support with simplified constraint specification.
73
74
```groovy { .api }
75
/**
76
* GridBag layout with integrated constraint handling
77
*/
78
class GridBagFactory {
79
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
80
static void processGridBagConstraintsAttributes(FactoryBuilderSupport builder, Object node, Map attributes)
81
}
82
83
// GridBag layout declaration
84
gridBagLayout()
85
86
// GridBag constraints as separate component or inline attributes
87
gridBagConstraints(gridx: int = GridBagConstraints.RELATIVE,
88
gridy: int = GridBagConstraints.RELATIVE,
89
gridwidth: int = 1, gridheight: int = 1,
90
weightx: double = 0.0, weighty: double = 0.0,
91
anchor: int = GridBagConstraints.CENTER,
92
fill: int = GridBagConstraints.NONE,
93
insets: Insets = new Insets(0,0,0,0),
94
ipadx: int = 0, ipady: int = 0)
95
96
// Shortcut alias
97
gbc(gridx: int, gridy: int, weightx: double = 0.0, weighty: double = 0.0)
98
```
99
100
**Usage Examples:**
101
102
```groovy
103
// Complex form layout with GridBag
104
panel {
105
gridBagLayout()
106
107
label(text: 'Name:', gridx: 0, gridy: 0, anchor: GridBagConstraints.EAST)
108
textField(columns: 20, gridx: 1, gridy: 0, weightx: 1.0, fill: GridBagConstraints.HORIZONTAL)
109
110
label(text: 'Email:', gridx: 0, gridy: 1, anchor: GridBagConstraints.EAST)
111
textField(columns: 20, gridx: 1, gridy: 1, weightx: 1.0, fill: GridBagConstraints.HORIZONTAL)
112
113
button(text: 'Submit', gridx: 1, gridy: 2, anchor: GridBagConstraints.EAST)
114
}
115
116
// Using separate constraint objects
117
panel {
118
gridBagLayout()
119
120
gbc(gridx: 0, gridy: 0, weightx: 0.0)
121
label(text: 'Label')
122
123
gbc(gridx: 1, gridy: 0, weightx: 1.0, fill: GridBagConstraints.HORIZONTAL)
124
textField(columns: 20)
125
}
126
```
127
128
### Box Layout
129
130
Support for BoxLayout and Box container with spacing components.
131
132
```groovy { .api }
133
/**
134
* Box layout for single-axis arrangement
135
*/
136
class BoxLayoutFactory {
137
BoxLayoutFactory()
138
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
139
}
140
141
// Box layout with axis specification
142
boxLayout(axis: int = BoxLayout.X_AXIS) // BoxLayout.X_AXIS, Y_AXIS, LINE_AXIS, PAGE_AXIS
143
144
/**
145
* Box container factory
146
*/
147
class BoxFactory {
148
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
149
}
150
151
// Box containers
152
box(axis: int = BoxLayout.X_AXIS)
153
hbox() // Horizontal box
154
vbox() // Vertical box
155
156
// Spacing components
157
hglue() // Horizontal glue (expandable space)
158
vglue() // Vertical glue
159
hstrut(width: int) // Fixed horizontal space
160
vstrut(height: int) // Fixed vertical space
161
glue() // General glue component
162
rigidArea(width: int, height: int) // Fixed size area
163
```
164
165
**Usage Examples:**
166
167
```groovy
168
// Horizontal button layout with spacing
169
hbox {
170
button(text: 'First')
171
hstrut(10) // 10 pixel gap
172
button(text: 'Second')
173
hglue() // Push remaining buttons to right
174
button(text: 'Last')
175
}
176
177
// Vertical layout with flexible spacing
178
vbox {
179
label(text: 'Header')
180
vstrut(5)
181
panel {
182
// main content
183
}
184
vglue() // Flexible space
185
hbox {
186
hglue()
187
button(text: 'OK')
188
hstrut(5)
189
button(text: 'Cancel')
190
}
191
}
192
193
// Custom box with specific axis
194
box(axis: BoxLayout.PAGE_AXIS) {
195
(1..3).each { i ->
196
button(text: "Item $i")
197
if (i < 3) rigidArea(width: 0, height: 10)
198
}
199
}
200
```
201
202
### Table Layout
203
204
Custom table-based layout manager for HTML-like table arrangement.
205
206
```groovy { .api }
207
/**
208
* Custom table-based layout manager
209
*/
210
class TableLayout implements LayoutManager2 {
211
TableLayout()
212
void addLayoutComponent(Component comp, Object constraints)
213
void removeLayoutComponent(Component comp)
214
Dimension preferredLayoutSize(Container parent)
215
Dimension minimumLayoutSize(Container parent)
216
void layoutContainer(Container parent)
217
}
218
219
/**
220
* Table layout factory
221
*/
222
class TableLayoutFactory {
223
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
224
}
225
226
/**
227
* Table row factory
228
*/
229
class TRFactory {
230
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
231
}
232
233
/**
234
* Table cell factory
235
*/
236
class TDFactory {
237
Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes)
238
}
239
240
// Table layout structure
241
tableLayout {
242
tr { // Table row
243
td { /* cell content */ } // Table cell
244
td { /* cell content */ }
245
}
246
tr {
247
td { /* cell content */ }
248
td { /* cell content */ }
249
}
250
}
251
```
252
253
**Usage Examples:**
254
255
```groovy
256
// Form layout using table structure
257
panel {
258
tableLayout {
259
tr {
260
td { label(text: 'First Name:') }
261
td { textField(columns: 15) }
262
}
263
tr {
264
td { label(text: 'Last Name:') }
265
td { textField(columns: 15) }
266
}
267
tr {
268
td { label(text: 'Email:') }
269
td { textField(columns: 20) }
270
}
271
tr {
272
td { /* empty */ }
273
td {
274
hbox {
275
button(text: 'Save')
276
hstrut(5)
277
button(text: 'Cancel')
278
}
279
}
280
}
281
}
282
}
283
284
// Complex layout with mixed content
285
panel {
286
tableLayout {
287
tr {
288
td {
289
scrollPane {
290
tree(id: 'categoryTree')
291
}
292
}
293
td {
294
splitPane(orientation: JSplitPane.VERTICAL_SPLIT) {
295
scrollPane { table(id: 'itemsTable') }
296
scrollPane { textArea(id: 'detailsArea') }
297
}
298
}
299
}
300
}
301
}
302
```
303
304
### Layout Constraints
305
306
Unified constraint handling system for all layout managers.
307
308
```groovy { .api }
309
/**
310
* Layout constraint attribute delegate
311
*/
312
static void constraintsAttributeDelegate(FactoryBuilderSupport builder, Object node, Map attributes) {
313
// Handles constraints attribute for all layout managers
314
}
315
316
// Layout-specific constraint examples:
317
318
// BorderLayout constraints
319
component(constraints: BorderLayout.NORTH)
320
component(constraints: 'North') // String form
321
322
// GridBagLayout constraints
323
component(gridx: 0, gridy: 0, weightx: 1.0, fill: GridBagConstraints.HORIZONTAL)
324
325
// CardLayout constraints
326
component(constraints: 'cardName')
327
328
// Custom constraints object
329
component(constraints: new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
330
GridBagConstraints.WEST,
331
GridBagConstraints.HORIZONTAL,
332
new Insets(5, 5, 5, 5), 0, 0))
333
```
334
335
### Layout Utilities
336
337
Helper methods and utilities for layout management.
338
339
```groovy { .api }
340
// Component alignment and sizing utilities
341
component.preferredSize = [width, height]
342
component.minimumSize = [width, height]
343
component.maximumSize = [width, height]
344
345
// Layout invalidation and revalidation
346
container.invalidate()
347
container.validate()
348
container.revalidate()
349
350
// Layout debugging
351
container.setComponentZOrder(component, index)
352
component.alignmentX = Component.CENTER_ALIGNMENT
353
component.alignmentY = Component.CENTER_ALIGNMENT
354
```
355
356
**Usage Examples:**
357
358
```groovy
359
// Setting component sizes
360
button(text: 'Fixed Size Button',
361
preferredSize: [150, 30],
362
minimumSize: [100, 25],
363
maximumSize: [200, 35])
364
365
// Dynamic layout updates
366
def dynamicPanel = panel {
367
flowLayout()
368
button(text: 'Add Component', actionPerformed: { e ->
369
dynamicPanel.add(new JButton("Dynamic ${dynamicPanel.componentCount}"))
370
dynamicPanel.revalidate()
371
dynamicPanel.repaint()
372
})
373
}
374
375
// Alignment in BoxLayout
376
vbox {
377
button(text: 'Left', alignmentX: Component.LEFT_ALIGNMENT)
378
button(text: 'Center', alignmentX: Component.CENTER_ALIGNMENT)
379
button(text: 'Right', alignmentX: Component.RIGHT_ALIGNMENT)
380
}
381
```
382
383
### Layout Factories Summary
384
385
Complete list of available layout factories and their purposes.
386
387
```groovy { .api }
388
// Standard Swing layouts
389
borderLayout() // BorderLayout
390
cardLayout() // CardLayout
391
flowLayout() // FlowLayout
392
gridLayout() // GridLayout
393
overlayLayout() // OverlayLayout
394
springLayout() // SpringLayout
395
396
// Enhanced layouts
397
gridBagLayout() // GridBagLayout with constraint support
398
boxLayout() // BoxLayout with axis specification
399
400
// Custom layouts
401
tableLayout() // HTML-like table layout
402
403
// Container factories that affect layout
404
box() // Box container
405
hbox() // Horizontal Box
406
vbox() // Vertical Box
407
408
// Spacing components for BoxLayout
409
glue() // Expandable space
410
hglue() // Horizontal expandable space
411
vglue() // Vertical expandable space
412
hstrut(width) // Fixed horizontal space
413
vstrut(height) // Fixed vertical space
414
rigidArea(width, height) // Fixed rectangular space
415
```