0
# Layout Management
1
2
Core layout manager implementation providing constraint-based positioning and sizing for Swing containers with support for string-based and programmatic constraint definition.
3
4
## Capabilities
5
6
### MigLayout Class
7
8
Primary layout manager implementing LayoutManager2 for Swing container integration.
9
10
```java { .api }
11
/**
12
* A very flexible layout manager for Swing containers.
13
* Implements LayoutManager2 and Externalizable for full Swing integration.
14
*/
15
public class MigLayout implements LayoutManager2, Externalizable {
16
17
/** Constructor with no constraints. */
18
public MigLayout();
19
20
/**
21
* Constructor with layout constraints only.
22
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as "".
23
*/
24
public MigLayout(String layoutConstraints);
25
26
/**
27
* Constructor with layout and column constraints.
28
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as "".
29
* @param colConstraints The constraints for the columns in the grid. null will be treated as "".
30
*/
31
public MigLayout(String layoutConstraints, String colConstraints);
32
33
/**
34
* Constructor with all string constraints.
35
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as "".
36
* @param colConstraints The constraints for the columns in the grid. null will be treated as "".
37
* @param rowConstraints The constraints for the rows in the grid. null will be treated as "".
38
*/
39
public MigLayout(String layoutConstraints, String colConstraints, String rowConstraints);
40
41
/**
42
* Constructor with LC object.
43
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as an empty constraint.
44
*/
45
public MigLayout(LC layoutConstraints);
46
47
/**
48
* Constructor with LC and AC objects.
49
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as an empty constraint.
50
* @param colConstraints The constraints for the columns in the grid. null will be treated as an empty constraint.
51
*/
52
public MigLayout(LC layoutConstraints, AC colConstraints);
53
54
/**
55
* Constructor with all constraint objects.
56
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as an empty constraint.
57
* @param colConstraints The constraints for the columns in the grid. null will be treated as an empty constraint.
58
* @param rowConstraints The constraints for the rows in the grid. null will be treated as an empty constraint.
59
*/
60
public MigLayout(LC layoutConstraints, AC colConstraints, AC rowConstraints);
61
}
62
```
63
64
**Usage Examples:**
65
66
```java
67
import net.miginfocom.swing.MigLayout;
68
import net.miginfocom.layout.LC;
69
import net.miginfocom.layout.AC;
70
import javax.swing.*;
71
72
// String-based approach
73
MigLayout stringLayout = new MigLayout(
74
"wrap 3, insets 10", // Layout constraints
75
"[grow][100:100:150][grow]", // Column constraints
76
"[]10[]" // Row constraints
77
);
78
79
// Programmatic approach
80
LC layoutConstraints = new LC()
81
.wrapAfter(3)
82
.insets("10");
83
AC colConstraints = new AC()
84
.grow()
85
.size("100:100:150")
86
.grow();
87
AC rowConstraints = new AC()
88
.gap("10");
89
90
MigLayout programmaticLayout = new MigLayout(layoutConstraints, colConstraints, rowConstraints);
91
92
// Apply to container
93
JPanel panel = new JPanel(stringLayout);
94
```
95
96
### Constraint Management
97
98
Methods for getting and setting layout, column, and row constraints at runtime.
99
100
```java { .api }
101
/**
102
* Returns layout constraints either as a String or LC depending what was sent in to the constructor.
103
* @return The layout constraints either as a String or LC. Never null.
104
*/
105
public Object getLayoutConstraints();
106
107
/**
108
* Sets the layout constraints for the layout manager instance.
109
* @param constr The layout constraints as a String or LC representation. null is converted to "" for storage.
110
* @throws RuntimeException if the constraint was not valid.
111
*/
112
public void setLayoutConstraints(Object constr);
113
114
/**
115
* Returns the column layout constraints either as a String or AC.
116
* @return The column constraints either as a String or AC. Never null.
117
*/
118
public Object getColumnConstraints();
119
120
/**
121
* Sets the column layout constraints for the layout manager instance.
122
* @param constr The column layout constraints as a String or AC representation. null is converted to "" for storage.
123
* @throws RuntimeException if the constraint was not valid.
124
*/
125
public void setColumnConstraints(Object constr);
126
127
/**
128
* Returns the row layout constraints either as a String or AC.
129
* @return The row constraints either as a String or AC. Never null.
130
*/
131
public Object getRowConstraints();
132
133
/**
134
* Sets the row layout constraints for the layout manager instance.
135
* @param constr The row layout constraints as a String or AC representation. null is converted to "" for storage.
136
* @throws RuntimeException if the constraint was not valid.
137
*/
138
public void setRowConstraints(Object constr);
139
```
140
141
### Component Constraint Management
142
143
Methods for managing individual component constraints within the layout.
144
145
```java { .api }
146
/**
147
* Returns a shallow copy of the constraints map.
148
* @return A shallow copy of the constraints map. Never null.
149
*/
150
public Map<Component, Object> getConstraintMap();
151
152
/**
153
* Sets the constraints map.
154
* @param map The map. Will be copied.
155
*/
156
public void setConstraintMap(Map<Component, Object> map);
157
158
/**
159
* Returns the component constraints as a String representation.
160
* @param comp The component to return the constraints for.
161
* @return The component constraints as a String representation or null if the component is not registered.
162
* The returned values is either a String or a CC depending on what constraint was sent in when the component was added.
163
*/
164
public Object getComponentConstraints(Component comp);
165
166
/**
167
* Sets the component constraint for the component that already must be handled by this layout manager.
168
* @param comp The component to set the constraints for.
169
* @param constr The component constraints as a String or CC. null is ok.
170
* @throws RuntimeException if the constraint was not valid.
171
* @throws IllegalArgumentException If the component is not handling the component.
172
*/
173
public void setComponentConstraints(Component comp, Object constr);
174
175
/**
176
* Returns if this layout manager is currently managing this component.
177
* @param c The component to check. If null then false will be returned.
178
* @return If this layout manager is currently managing this component.
179
*/
180
public boolean isManagingComponent(Component c);
181
```
182
183
### Layout Callbacks
184
185
Methods for adding and removing layout callback functions that are called at different stages of the layout cycle.
186
187
```java { .api }
188
/**
189
* Adds the callback function that will be called at different stages of the layout cycle.
190
* @param callback The callback. Not null.
191
*/
192
public void addLayoutCallback(LayoutCallback callback);
193
194
/**
195
* Removes the callback if it exists.
196
* @param callback The callback. May be null.
197
*/
198
public void removeLayoutCallback(LayoutCallback callback);
199
```
200
201
### LayoutManager2 Implementation
202
203
Standard Swing layout manager interface implementation for container integration.
204
205
```java { .api }
206
/**
207
* Performs layout of the container.
208
* @param parent The container to lay out.
209
*/
210
public void layoutContainer(Container parent);
211
212
/**
213
* Returns the minimum layout size.
214
* @param parent The container to calculate minimum size for.
215
* @return The minimum dimension.
216
*/
217
public Dimension minimumLayoutSize(Container parent);
218
219
/**
220
* Returns the preferred layout size.
221
* @param parent The container to calculate preferred size for.
222
* @return The preferred dimension.
223
*/
224
public Dimension preferredLayoutSize(Container parent);
225
226
/**
227
* Returns the maximum layout size.
228
* @param parent The container to calculate maximum size for.
229
* @return The maximum dimension (Integer.MAX_VALUE for both width and height).
230
*/
231
public Dimension maximumLayoutSize(Container parent);
232
233
/**
234
* Returns the alignment along the X axis.
235
* @param parent The container.
236
* @return The X axis alignment.
237
*/
238
public float getLayoutAlignmentX(Container parent);
239
240
/**
241
* Returns the alignment along the Y axis.
242
* @param parent The container.
243
* @return The Y axis alignment.
244
*/
245
public float getLayoutAlignmentY(Container parent);
246
247
/**
248
* Adds a component to the layout with string constraint.
249
* @param s The constraint string.
250
* @param comp The component to add.
251
*/
252
public void addLayoutComponent(String s, Component comp);
253
254
/**
255
* Adds a component to the layout with object constraint.
256
* @param comp The component to add.
257
* @param constraints The constraint object (String or CC).
258
*/
259
public void addLayoutComponent(Component comp, Object constraints);
260
261
/**
262
* Removes a component from the layout.
263
* @param comp The component to remove.
264
*/
265
public void removeLayoutComponent(Component comp);
266
267
/**
268
* Invalidates the layout cache.
269
* @param target The container target.
270
*/
271
public void invalidateLayout(Container target);
272
```
273
274
### Utility Methods
275
276
Static utility methods for component hierarchy navigation.
277
278
```java { .api }
279
/**
280
* Finds parent component of specific type in the component hierarchy.
281
* @param clazz The class type to find.
282
* @param comp The starting component.
283
* @return The parent component of the specified type, or null if not found.
284
*/
285
public static <E> E findType(Class<E> clazz, Component comp);
286
```
287
288
### Serialization Support
289
290
Methods for object serialization and deserialization support.
291
292
```java { .api }
293
/**
294
* Reads the object state from the input stream for deserialization.
295
* @param in The ObjectInput to read from
296
* @throws IOException if I/O error occurs
297
* @throws ClassNotFoundException if class cannot be found during deserialization
298
*/
299
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;
300
301
/**
302
* Writes the object state to the output stream for serialization.
303
* @param out The ObjectOutput to write to
304
* @throws IOException if I/O error occurs
305
*/
306
public void writeExternal(ObjectOutput out) throws IOException;
307
```
308
309
**Usage Examples:**
310
311
```java
312
import net.miginfocom.swing.MigLayout;
313
import javax.swing.*;
314
import java.awt.*;
315
316
// Dynamic constraint modification
317
MigLayout layout = new MigLayout("wrap 2");
318
JPanel panel = new JPanel(layout);
319
320
JButton button = new JButton("Click me");
321
panel.add(button, "growx");
322
323
// Later modify constraints
324
layout.setComponentConstraints(button, "growx, span 2");
325
326
// Check if component is managed
327
if (layout.isManagingComponent(button)) {
328
// Component is managed by this layout
329
Object constraints = layout.getComponentConstraints(button);
330
}
331
332
// Find parent window
333
Window parentWindow = MigLayout.findType(Window.class, button);
334
335
// Serialization support
336
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("layout.ser"))) {
337
layout.writeExternal(out);
338
}
339
340
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("layout.ser"))) {
341
MigLayout restoredLayout = new MigLayout();
342
restoredLayout.readExternal(in);
343
}
344
```