0
# Layout Management
1
2
Groovy Swing provides factory methods for all major Swing layout managers, enabling declarative layout configuration.
3
4
## Standard Layout Managers
5
6
Core Swing layout managers for organizing components.
7
8
```groovy { .api }
9
// Basic layouts
10
def borderLayout(Map attributes = [:])
11
def flowLayout(Map attributes = [:])
12
def gridLayout(Map attributes = [:])
13
def cardLayout(Map attributes = [:])
14
15
// Advanced layouts
16
def gridBagLayout(Map attributes = [:])
17
def springLayout(Map attributes = [:])
18
def overlayLayout(Map attributes = [:])
19
20
// GridBag constraints
21
def gridBagConstraints(Map attributes = [:])
22
def gbc(Map attributes = [:]) // Shortcut for gridBagConstraints
23
```
24
25
### Standard Layout Examples
26
27
```groovy
28
// Border layout
29
swing.panel {
30
borderLayout(hgap: 5, vgap: 5)
31
button(text: 'North', constraints: BorderLayout.NORTH)
32
button(text: 'South', constraints: BorderLayout.SOUTH)
33
button(text: 'East', constraints: BorderLayout.EAST)
34
button(text: 'West', constraints: BorderLayout.WEST)
35
button(text: 'Center', constraints: BorderLayout.CENTER)
36
}
37
38
// Grid layout
39
swing.panel {
40
gridLayout(rows: 2, columns: 3, hgap: 5, vgap: 5)
41
(1..6).each { i ->
42
button(text: "Button $i")
43
}
44
}
45
46
// Flow layout
47
swing.panel {
48
flowLayout(alignment: FlowLayout.CENTER, hgap: 10)
49
button(text: 'Left')
50
button(text: 'Center')
51
button(text: 'Right')
52
}
53
54
// Card layout with switching
55
swing.panel {
56
cardLayout()
57
panel(constraints: 'card1') {
58
label(text: 'Card 1 Content')
59
}
60
panel(constraints: 'card2') {
61
label(text: 'Card 2 Content')
62
}
63
}
64
```
65
66
### GridBag Layout
67
68
GridBagLayout with constraint support for complex layouts.
69
70
```groovy
71
swing.panel {
72
gridBagLayout()
73
74
label(text: 'Name:', constraints: gbc(gridx: 0, gridy: 0, anchor: GridBagConstraints.EAST))
75
textField(constraints: gbc(gridx: 1, gridy: 0, fill: GridBagConstraints.HORIZONTAL, weightx: 1.0))
76
77
label(text: 'Email:', constraints: gbc(gridx: 0, gridy: 1, anchor: GridBagConstraints.EAST))
78
textField(constraints: gbc(gridx: 1, gridy: 1, fill: GridBagConstraints.HORIZONTAL, weightx: 1.0))
79
80
button(text: 'Submit', constraints: gbc(gridx: 1, gridy: 2, anchor: GridBagConstraints.EAST))
81
}
82
```
83
84
## Box Layout
85
86
Box layout and related components for linear arrangements.
87
88
```groovy { .api }
89
// Box layout manager
90
def boxLayout(Map attributes = [:])
91
92
// Box containers
93
def box(Map attributes = [:], Closure closure = null)
94
def hbox(Map attributes = [:], Closure closure = null) // Horizontal box
95
def vbox(Map attributes = [:], Closure closure = null) // Vertical box
96
97
// Spacing components
98
def glue(Map attributes = [:])
99
def hglue(Map attributes = [:]) // Horizontal glue
100
def vglue(Map attributes = [:]) // Vertical glue
101
def hstrut(Map attributes = [:]) // Horizontal strut
102
def vstrut(Map attributes = [:]) // Vertical strut
103
def rigidArea(Map attributes = [:])
104
```
105
106
### Box Layout Examples
107
108
```groovy
109
// Horizontal box with spacing
110
swing.hbox {
111
button(text: 'Left')
112
hglue() // Push remaining items to right
113
button(text: 'Center')
114
hglue()
115
button(text: 'Right')
116
}
117
118
// Vertical box with struts
119
swing.vbox {
120
button(text: 'Top')
121
vstrut(height: 10) // Fixed spacing
122
button(text: 'Middle')
123
vglue() // Variable spacing
124
button(text: 'Bottom')
125
}
126
127
// Custom box layout
128
swing.panel {
129
boxLayout(axis: BoxLayout.Y_AXIS)
130
button(text: 'Button 1')
131
rigidArea(width: 5, height: 5) // Fixed spacer
132
button(text: 'Button 2')
133
}
134
135
// Nested boxes for complex layouts
136
swing.vbox {
137
hbox {
138
button(text: '1')
139
button(text: '2')
140
hglue()
141
button(text: '3')
142
}
143
vstrut(height: 10)
144
hbox {
145
hglue()
146
button(text: '4')
147
button(text: '5')
148
}
149
}
150
```
151
152
## Table Layout
153
154
Custom table-based layout manager with row and cell support.
155
156
```groovy { .api }
157
// Table layout manager
158
def tableLayout(Map attributes = [:])
159
160
// Table structure
161
def tr(Map attributes = [:], Closure closure = null) // Table row
162
def td(Map attributes = [:], Closure closure = null) // Table cell
163
```
164
165
### Table Layout Examples
166
167
```groovy
168
swing.panel {
169
tableLayout {
170
tr {
171
td { label(text: 'Name:') }
172
td { textField(columns: 20) }
173
}
174
tr {
175
td { label(text: 'Email:') }
176
td { textField(columns: 20) }
177
}
178
tr {
179
td(colspan: 2) {
180
button(text: 'Submit')
181
}
182
}
183
}
184
}
185
186
// Table layout with spacing and alignment
187
swing.panel {
188
tableLayout(
189
columnSpacing: 5,
190
rowSpacing: 5
191
) {
192
tr(alignment: 'center') {
193
td(width: 100) { label(text: 'Label 1') }
194
td(width: 200) { textField() }
195
td { button(text: 'Browse') }
196
}
197
tr {
198
td { label(text: 'Label 2') }
199
td(colspan: 2) { textArea(rows: 3) }
200
}
201
}
202
}
203
```
204
205
## Layout Constraints
206
207
Layout constraint handling for various layout managers.
208
209
```groovy { .api }
210
// Constraint delegation - automatically handled by layout factories
211
// Components can specify constraints via 'constraints' attribute
212
```
213
214
### Constraint Examples
215
216
```groovy
217
// Border layout constraints
218
swing.panel {
219
borderLayout()
220
button(text: 'North', constraints: BorderLayout.NORTH)
221
button(text: 'Center', constraints: BorderLayout.CENTER)
222
}
223
224
// GridBag constraints with direct attributes
225
swing.panel {
226
gridBagLayout()
227
button(
228
text: 'Stretch Button',
229
constraints: gbc(
230
gridx: 0, gridy: 0,
231
gridwidth: 2, gridheight: 1,
232
weightx: 1.0, weighty: 0.0,
233
fill: GridBagConstraints.HORIZONTAL,
234
anchor: GridBagConstraints.NORTHWEST,
235
insets: new Insets(5, 5, 5, 5)
236
)
237
)
238
}
239
240
// Split pane constraints
241
swing.splitPane {
242
panel(constraints: 'left') { /* left content */ }
243
panel(constraints: 'right') { /* right content */ }
244
}
245
246
// Tabbed pane constraints (title and optional icon)
247
swing.tabbedPane {
248
panel(title: 'Tab 1', icon: myIcon) { /* tab content */ }
249
panel(title: 'Tab 2') { /* tab content */ }
250
}
251
```
252
253
## Layout Factory Implementation
254
255
The layout factory system provides automatic constraint delegation.
256
257
```groovy { .api }
258
// Layout factories automatically handle:
259
// - Layout manager creation and configuration
260
// - Constraint attribute delegation
261
// - Component constraint application
262
// - Parent-child relationships
263
```
264
265
### Advanced Layout Techniques
266
267
```groovy
268
// Combining layouts for complex UIs
269
swing.frame(title: 'Complex Layout') {
270
borderLayout()
271
272
// Top toolbar
273
toolBar(constraints: BorderLayout.NORTH) {
274
button(text: 'New')
275
button(text: 'Open')
276
separator()
277
button(text: 'Save')
278
}
279
280
// Main content area
281
splitPane(constraints: BorderLayout.CENTER, orientation: JSplitPane.HORIZONTAL_SPLIT) {
282
// Left sidebar
283
panel(constraints: 'left') {
284
borderLayout()
285
label(text: 'Sidebar', constraints: BorderLayout.NORTH)
286
scrollPane(constraints: BorderLayout.CENTER) {
287
tree()
288
}
289
}
290
291
// Right content
292
tabbedPane(constraints: 'right') {
293
panel(title: 'Document 1') {
294
borderLayout()
295
scrollPane(constraints: BorderLayout.CENTER) {
296
textArea()
297
}
298
}
299
panel(title: 'Document 2') {
300
vbox {
301
hbox {
302
label(text: 'Form Field:')
303
textField()
304
}
305
vstrut(height: 10)
306
scrollPane {
307
table()
308
}
309
}
310
}
311
}
312
}
313
314
// Status bar
315
label(text: 'Ready', constraints: BorderLayout.SOUTH)
316
}
317
```