0
# Constraint Configuration
1
2
Structured constraint objects from the core module providing programmatic layout configuration as type-safe alternatives to string-based constraints. These classes enable detailed control over layout behavior, column/row specifications, and individual component positioning through fluent builder APIs.
3
4
## Capabilities
5
6
### LC (Layout Constraints)
7
8
Layout constraint object containing configuration for the entire layout manager instance with both property-based and fluent builder APIs.
9
10
```java { .api }
11
/**
12
* Contains the constraints for an instance of the MigLayout layout manager.
13
* Provides programmatic alternative to string-based layout constraints.
14
*/
15
public class LC implements Externalizable {
16
17
/**
18
* Empty constructor creating default layout constraints.
19
*/
20
public LC();
21
}
22
```
23
24
#### Property-Based Configuration
25
26
Direct property access methods for layout configuration.
27
28
```java { .api }
29
// Cache control
30
public boolean isNoCache();
31
public void setNoCache(boolean b);
32
33
// Flow direction
34
public final boolean isFlowX();
35
public final void setFlowX(boolean b);
36
37
// Fill behavior
38
public final boolean isFillX();
39
public final void setFillX(boolean b);
40
public final boolean isFillY();
41
public final void setFillY(boolean b);
42
43
// Alignment
44
public final UnitValue getAlignX();
45
public final void setAlignX(UnitValue uv);
46
public final UnitValue getAlignY();
47
public final void setAlignY(UnitValue uv);
48
49
// Layout flow
50
public final boolean isTopToBottom();
51
public final void setTopToBottom(boolean b);
52
public final Boolean getLeftToRight();
53
public final void setLeftToRight(Boolean b);
54
55
// Visual padding
56
public final boolean isVisualPadding();
57
public final void setVisualPadding(boolean b);
58
59
// Grid behavior
60
public final boolean isNoGrid();
61
public final void setNoGrid(boolean b);
62
63
// Wrap behavior
64
public final int getWrapAfter();
65
public final void setWrapAfter(int count);
66
67
// Hide mode
68
public final int getHideMode();
69
public final void setHideMode(int mode);
70
71
// Debug support
72
public final int getDebugMillis();
73
public final void setDebugMillis(int millis);
74
75
// Insets
76
public final UnitValue[] getInsets();
77
public final void setInsets(UnitValue[] ins);
78
79
// Grid gaps
80
public final BoundSize getGridGapX();
81
public final void setGridGapX(BoundSize x);
82
public final BoundSize getGridGapY();
83
public final void setGridGapY(BoundSize y);
84
85
// Container sizing
86
public final BoundSize getWidth();
87
public final void setWidth(BoundSize size);
88
public final BoundSize getHeight();
89
public final void setHeight(BoundSize size);
90
91
// Pack sizing
92
public final BoundSize getPackWidth();
93
public final void setPackWidth(BoundSize size);
94
public final BoundSize getPackHeight();
95
public final void setPackHeight(BoundSize size);
96
public final float getPackWidthAlign();
97
public final void setPackWidthAlign(float align);
98
public final float getPackHeightAlign();
99
public final void setPackHeightAlign(float align);
100
```
101
102
#### Fluent Builder API
103
104
Chainable configuration methods returning `this` for method chaining.
105
106
```java { .api }
107
// Basic layout
108
public final LC wrap();
109
public final LC wrapAfter(int count);
110
public final LC noCache();
111
public final LC flowX();
112
public final LC flowY();
113
public final LC fill();
114
public final LC fillX();
115
public final LC fillY();
116
public final LC leftToRight(boolean b);
117
public final LC rightToLeft();
118
public final LC topToBottom();
119
public final LC bottomToTop();
120
public final LC noGrid();
121
public final LC noVisualPadding();
122
123
// Pack sizing
124
public final LC pack();
125
public final LC pack(String width, String height);
126
public final LC packAlign(float alignX, float alignY);
127
128
// Container sizing
129
public final LC minWidth(String width);
130
public final LC width(String width);
131
public final LC maxWidth(String width);
132
public final LC minHeight(String height);
133
public final LC height(String height);
134
public final LC maxHeight(String height);
135
136
// Insets
137
public final LC insetsAll(String allSides);
138
public final LC insets(String s);
139
public final LC insets(String top, String left, String bottom, String right);
140
141
// Alignment
142
public final LC alignX(String align);
143
public final LC alignX(AlignX align);
144
public final LC alignY(String align);
145
public final LC alignY(AlignY align);
146
public final LC align(String ax, String ay);
147
148
// Grid gaps
149
public final LC gridGapX(String boundsSize);
150
public final LC gridGapY(String boundsSize);
151
public final LC gridGap(String gapx, String gapy);
152
153
// Debug
154
public final LC debug();
155
public final LC debug(int repaintMillis);
156
157
// Hide mode
158
public final LC hideMode(HideMode mode);
159
public final LC hideMode(int mode);
160
```
161
162
### AC (Axis Constraints)
163
164
Axis constraint object holding column or row constraints with fluent builder API for grid configuration.
165
166
```java { .api }
167
/**
168
* A constraint that holds the column or row constraints for the grid.
169
* This class is a holder and builder for a number of DimConstraints.
170
*/
171
public class AC implements Externalizable {
172
173
/**
174
* Constructor creating instance that can be configured manually.
175
* Will be initialized with a default DimConstraint.
176
*/
177
public AC();
178
179
/**
180
* Returns the different DimConstraints that this object consists of.
181
* @return The different DimConstraints. A new array and never null.
182
*/
183
public final DimConstraint[] getConstraints();
184
185
/**
186
* Sets the different DimConstraints that this object should consist of.
187
* @param constr The different DimConstraints. The array will be copied for storage.
188
* null or empty array will reset the constraints to one DimConstraint with default values.
189
*/
190
public final void setConstraints(DimConstraint[] constr);
191
192
/**
193
* Returns the number of DimConstraints.
194
* @return The number of DimConstraints.
195
*/
196
public int getCount();
197
}
198
```
199
200
#### Fluent Builder Methods
201
202
Chainable configuration methods for axis constraints.
203
204
```java { .api }
205
// Basic operations
206
public final AC count(int size);
207
public final AC noGrid();
208
public final AC noGrid(int... indexes);
209
public final AC index(int i);
210
public final AC fill();
211
public final AC fill(int... indexes);
212
213
// Size configuration
214
public final AC size(String s);
215
public final AC size(String size, int... indexes);
216
217
// Gap configuration
218
public final AC gap();
219
public final AC gap(String size);
220
public final AC gap(String size, int... indexes);
221
222
// Alignment
223
public final AC align(String side);
224
public final AC align(String side, int... indexes);
225
226
// Size groups
227
public final AC sizeGroup();
228
public final AC sizeGroup(String s);
229
public final AC sizeGroup(String s, int... indexes);
230
231
// Grow behavior
232
public final AC growPrio(int p);
233
public final AC growPrio(int p, int... indexes);
234
public final AC grow();
235
public final AC grow(float w);
236
public final AC grow(float w, int... indexes);
237
238
// Shrink behavior
239
public final AC shrinkPrio(int p);
240
public final AC shrinkPrio(int p, int... indexes);
241
public final AC shrink();
242
public final AC shrink(float w);
243
public final AC shrink(float w, int... indexes);
244
245
// Deprecated methods (still part of public API)
246
@Deprecated
247
public final DimConstraint[] getConstaints();
248
@Deprecated
249
public final void setConstaints(DimConstraint[] constr);
250
@Deprecated
251
public final AC shrinkWeight(float w);
252
@Deprecated
253
public final AC shrinkWeight(float w, int... indexes);
254
```
255
256
### CC (Component Constraints)
257
258
Component constraint object for individual component positioning and sizing with comprehensive fluent builder API.
259
260
```java { .api }
261
/**
262
* A simple value holder for one component's constraint.
263
* Provides programmatic alternative to string-based component constraints.
264
*/
265
public class CC implements Externalizable {
266
267
/**
268
* Empty constructor creating default component constraints.
269
*/
270
public CC();
271
}
272
```
273
274
#### Property Access Methods
275
276
Direct property access for component constraints.
277
278
```java { .api }
279
// Position and cell
280
public int getCellX();
281
public void setCellX(int x);
282
public int getCellY();
283
public void setCellY(int y);
284
public UnitValue[] getPos();
285
public void setPos(UnitValue[] pos);
286
287
// Span
288
public int getSpanX();
289
public void setSpanX(int cells);
290
public int getSpanY();
291
public void setSpanY(int cells);
292
293
// Docking
294
public int getDockSide();
295
public void setDockSide(int side);
296
297
// Flow
298
public Boolean getFlowX();
299
public void setFlowX(Boolean b);
300
301
// Skip and split
302
public int getSkip();
303
public void setSkip(int cells);
304
public int getSplit();
305
public void setSplit(int parts);
306
307
// Hide mode
308
public int getHideMode();
309
public void setHideMode(int mode);
310
311
// ID and tag
312
public String getId();
313
public void setId(String id);
314
public String getTag();
315
public void setTag(String tag);
316
317
// Push weights
318
public Float getPushX();
319
public void setPushX(Float weight);
320
public Float getPushY();
321
public void setPushY(Float weight);
322
323
// Padding
324
public UnitValue[] getPadding();
325
public void setPadding(UnitValue[] sides);
326
public UnitValue[] getVisualPadding();
327
public void setVisualPadding(UnitValue[] sides);
328
329
// Bounds and external
330
public boolean isBoundsInGrid();
331
public void setBoundsInGrid(boolean b);
332
public boolean isExternal();
333
public void setExternal(boolean b);
334
335
// Wrap and newline
336
public boolean isWrap();
337
public void setWrap(boolean b);
338
public BoundSize getWrapGapSize();
339
public void setWrapGapSize(BoundSize s);
340
public boolean isNewline();
341
public void setNewline(boolean b);
342
public BoundSize getNewlineGapSize();
343
public void setNewlineGapSize(BoundSize s);
344
345
// Dimension constraints
346
public DimConstraint getHorizontal();
347
public void setHorizontal(DimConstraint h);
348
public DimConstraint getVertical();
349
public void setVertical(DimConstraint v);
350
public DimConstraint getDimConstraint(boolean isHor);
351
352
// Animation
353
public AnimSpec getAnimSpec();
354
```
355
356
#### Fluent Builder Methods
357
358
Comprehensive fluent API for component constraint configuration.
359
360
```java { .api }
361
// Size groups
362
public final CC endGroupX(String s);
363
public final CC endGroupY(String s);
364
public final CC endGroup(String ... xy);
365
public final CC sizeGroupX(String s);
366
public final CC sizeGroupY(String s);
367
public final CC sizeGroup(String ... xy);
368
369
// Width and height
370
public final CC minWidth(String size);
371
public final CC width(String size);
372
public final CC maxWidth(String size);
373
public final CC minHeight(String size);
374
public final CC height(String size);
375
public final CC maxHeight(String size);
376
377
// Gaps
378
public final CC gapX(String before, String after);
379
public final CC gapY(String before, String after);
380
public final CC gap(String ... args);
381
public final CC gapBefore(String boundsSize);
382
public final CC gapAfter(String boundsSize);
383
public final CC gapTop(String boundsSize);
384
public final CC gapLeft(String boundsSize);
385
public final CC gapBottom(String boundsSize);
386
public final CC gapRight(String boundsSize);
387
388
// Alignment
389
public final CC alignX(String align);
390
public final CC alignX(AlignX align);
391
public final CC alignY(String align);
392
public final CC alignY(AlignY align);
393
394
// Grow behavior
395
public final CC growPrioX(int p);
396
public final CC growPrioY(int p);
397
public final CC growPrio(int ... widthHeight);
398
public final CC growX();
399
public final CC growX(float w);
400
public final CC growY();
401
public final CC growY(float w);
402
public final CC grow();
403
public final CC grow(float ... widthHeight);
404
405
// Deprecated methods (still part of public API)
406
@Deprecated
407
public final CC growY(Float w);
408
409
// Shrink behavior
410
public final CC shrinkPrioX(int p);
411
public final CC shrinkPrioY(int p);
412
public final CC shrinkPrio(int ... widthHeight);
413
public final CC shrinkX(float w);
414
public final CC shrinkY(float w);
415
public final CC shrink(float ... widthHeight);
416
417
// Cell position and span
418
public final CC cell(int ... colRowWidthHeight);
419
public final CC span(int ... cells);
420
public final CC spanX();
421
public final CC spanX(int cells);
422
public final CC spanY();
423
public final CC spanY(int cells);
424
425
// Push weights
426
public final CC push();
427
public final CC push(Float weightX, Float weightY);
428
public final CC pushX();
429
public final CC pushX(Float weight);
430
public final CC pushY();
431
public final CC pushY(Float weight);
432
433
// Split and skip
434
public final CC split();
435
public final CC split(int parts);
436
public final CC skip();
437
public final CC skip(int cells);
438
439
// Flow control
440
public final CC flowX();
441
public final CC flowY();
442
public final CC external();
443
444
// Wrap and newline
445
public final CC wrap();
446
public final CC wrap(String gapSize);
447
public final CC newline();
448
public final CC newline(String gapSize);
449
450
// Docking
451
public final CC dockNorth();
452
public final CC dockWest();
453
public final CC dockSouth();
454
public final CC dockEast();
455
456
// Absolute positioning
457
public final CC x(String x);
458
public final CC y(String y);
459
public final CC x2(String x2);
460
public final CC y2(String y2);
461
public final CC pos(String x, String y);
462
public final CC pos(String x, String y, String x2, String y2);
463
464
// Padding
465
public final CC pad(int top, int left, int bottom, int right);
466
public final CC pad(String pad);
467
468
// ID and tag
469
public final CC id(String s);
470
public final CC tag(String tag);
471
public final CC hideMode(int mode);
472
```
473
474
### DimConstraint
475
476
Individual dimension constraint used within AC objects for detailed column/row configuration.
477
478
```java { .api }
479
/**
480
* A constraint for one dimension (column or row).
481
* Contains size, gap, alignment, and other properties for a single grid dimension.
482
*/
483
public class DimConstraint implements Externalizable {
484
485
/**
486
* Empty constructor creating default dimension constraint.
487
*/
488
public DimConstraint();
489
490
// Size constraint
491
public BoundSize getSize();
492
public void setSize(BoundSize size);
493
494
// Grow/shrink behavior
495
public int getGrowPriority();
496
public void setGrowPriority(int p);
497
public Float getGrow();
498
public void setGrow(Float weight);
499
public int getShrinkPriority();
500
public void setShrinkPriority(int p);
501
public Float getShrink();
502
public void setShrink(Float weight);
503
504
// Alignment
505
public UnitValue getAlign();
506
public void setAlign(UnitValue uv);
507
public UnitValue getAlignOrDefault(boolean isCols);
508
509
// Gaps
510
public BoundSize getGapAfter();
511
public void setGapAfter(BoundSize size);
512
public BoundSize getGapBefore();
513
public void setGapBefore(BoundSize size);
514
515
// Groups
516
public String getSizeGroup();
517
public void setSizeGroup(String s);
518
public String getEndGroup();
519
public void setEndGroup(String s);
520
521
// Fill and grid behavior
522
public boolean isFill();
523
public void setFill(boolean b);
524
public boolean isNoGrid();
525
public void setNoGrid(boolean b);
526
}
527
```
528
529
### Enums
530
531
Important enumeration types used in constraint configuration.
532
533
```java { .api }
534
/**
535
* Horizontal alignment enumeration for X-axis positioning.
536
*/
537
public enum AlignX {
538
LEADING("leading"),
539
LEFT("left"),
540
CENTER("center"),
541
RIGHT("right"),
542
TRAILING("trailing");
543
544
/**
545
* Returns the string code for this alignment.
546
* @return The string representation used in constraints.
547
*/
548
public String code();
549
}
550
551
/**
552
* Vertical alignment enumeration for Y-axis positioning.
553
*/
554
public enum AlignY {
555
TOP("top"),
556
CENTER("center"),
557
BOTTOM("bottom"),
558
BASELINE("baseline");
559
560
/**
561
* Returns the string code for this alignment.
562
* @return The string representation used in constraints.
563
*/
564
public String code();
565
}
566
567
/**
568
* Hide mode enumeration for component visibility behavior.
569
*/
570
public enum HideMode {
571
NORMAL(0),
572
SIZE_0_RETAIN_GAPS(1),
573
SIZE_0_GAPS_0(2),
574
DISREGARD(3);
575
576
/**
577
* Returns the numeric code for this hide mode.
578
* @return The integer code used internally.
579
*/
580
public int getCode();
581
582
/**
583
* Returns the HideMode for the specified code.
584
* @param code The integer code to lookup.
585
* @return The corresponding HideMode enum value.
586
*/
587
public static HideMode of(int code);
588
}
589
```
590
591
### Constants
592
593
```java { .api }
594
// Dock sides constants in CC class
595
public static final String[] DOCK_SIDES = {"north", "west", "south", "east"};
596
```
597
598
**Usage Examples:**
599
600
```java
601
import net.miginfocom.layout.*;
602
import net.miginfocom.layout.AlignX;
603
import net.miginfocom.layout.AlignY;
604
import net.miginfocom.layout.HideMode;
605
import net.miginfocom.swing.MigLayout;
606
import javax.swing.*;
607
608
// Layout constraints using fluent API
609
LC layoutConstraints = new LC()
610
.flowX() // Horizontal flow
611
.fillY() // Fill vertically
612
.wrap() // Enable wrapping
613
.insets("10") // Set insets
614
.debug(); // Enable debug
615
616
// Column constraints using fluent API
617
AC columnConstraints = new AC()
618
.count(3) // Three columns
619
.fill(0) // First column fills
620
.size("100px", 1) // Second column fixed 100px
621
.grow(1.0f, 2); // Third column grows
622
623
// Row constraints
624
AC rowConstraints = new AC()
625
.gap("5px") // 5px gaps between rows
626
.align("top"); // Top alignment
627
628
// Create layout with programmatic constraints
629
MigLayout layout = new MigLayout(layoutConstraints, columnConstraints, rowConstraints);
630
JPanel panel = new JPanel(layout);
631
632
// Component constraints using fluent API
633
CC buttonConstraints = new CC()
634
.cell(0, 0, 2, 1) // Position and span
635
.pushX() // Push horizontally
636
.growX() // Grow horizontally
637
.id("submitButton") // Set ID for linking
638
.tag("form") // Set tag for grouping
639
.alignX(AlignX.CENTER); // Center alignment using enum
640
641
JButton button = new JButton("Submit");
642
panel.add(button, buttonConstraints);
643
644
// Complex component constraints with method chaining
645
CC textFieldConstraints = new CC()
646
.cell(0, 1) // Cell position
647
.spanX(3) // Span 3 columns
648
.growX() // Grow horizontally
649
.pad("5", "10", "5", "10") // Padding
650
.gapAfter("10px") // Gap after component
651
.hideMode(HideMode.SIZE_0_GAPS_0.getCode()) // Hide mode using enum
652
.sizeGroupX("inputs") // Size group
653
.pushX(0.5f); // Push weight
654
655
JTextField textField = new JTextField();
656
panel.add(textField, textFieldConstraints);
657
658
// Docking constraints
659
CC toolbarConstraints = new CC()
660
.dockNorth() // Dock to north
661
.growX(); // Grow horizontally
662
663
JToolBar toolbar = new JToolBar();
664
panel.add(toolbar, toolbarConstraints);
665
```