0
# MiGLayout Swing
1
2
MiGLayout Swing provides a constraint-based layout manager for Java Swing applications. It enables developers to create sophisticated layouts using string-based or programmatic constraints, supporting flowing, grid-based, absolute, grouped, and docking layouts with automatic per-platform fidelity.
3
4
## Package Information
5
6
- **Package Name**: miglayout-swing
7
- **Package Type**: maven
8
- **Language**: Java
9
- **Installation**: `<dependency><groupId>com.miglayout</groupId><artifactId>miglayout-swing</artifactId><version>11.4.1</version></dependency>`
10
- **Module**: `com.miglayout.swing`
11
12
## Core Imports
13
14
```java
15
import net.miginfocom.swing.MigLayout;
16
import net.miginfocom.layout.LC;
17
import net.miginfocom.layout.AC;
18
import net.miginfocom.layout.CC;
19
```
20
21
## Basic Usage
22
23
```java
24
import net.miginfocom.swing.MigLayout;
25
import javax.swing.*;
26
27
// Basic setup with string constraints
28
JPanel panel = new JPanel(new MigLayout("wrap 2", "[grow]", "[][]"));
29
30
// Add components with constraints
31
panel.add(new JLabel("Name:"), "align right");
32
panel.add(new JTextField(15), "growx");
33
panel.add(new JLabel("Email:"), "align right");
34
panel.add(new JTextField(15), "growx");
35
panel.add(new JButton("Submit"), "span 2, center");
36
37
// Alternative programmatic approach
38
MigLayout layout = new MigLayout();
39
layout.setLayoutConstraints("wrap 2");
40
layout.setColumnConstraints("[grow]");
41
layout.setRowConstraints("[][]");
42
43
JPanel programmaticPanel = new JPanel(layout);
44
```
45
46
## Architecture
47
48
MiGLayout Swing is built around several key components:
49
50
- **Layout Manager**: `MigLayout` class implementing `LayoutManager2` for Swing integration
51
- **Component Wrappers**: Adapter classes (`SwingComponentWrapper`, `SwingContainerWrapper`) that bridge Swing components with the MiGLayout engine
52
- **Constraint Objects**: Structured constraint classes (`LC`, `AC`, `CC`) from the core module for programmatic layout definition
53
- **Grid System**: Internal grid-based layout calculation engine with support for spanning, gaps, and alignment
54
- **Debug Visualization**: Built-in debug capabilities for layout troubleshooting and optimization
55
56
## Capabilities
57
58
### Layout Management
59
60
Primary layout manager implementation providing constraint-based positioning and sizing for Swing containers.
61
62
```java { .api }
63
public class MigLayout implements LayoutManager2, Externalizable {
64
// String-based constructors
65
public MigLayout();
66
public MigLayout(String layoutConstraints);
67
public MigLayout(String layoutConstraints, String colConstraints);
68
public MigLayout(String layoutConstraints, String colConstraints, String rowConstraints);
69
70
// Programmatic constructors
71
public MigLayout(LC layoutConstraints);
72
public MigLayout(LC layoutConstraints, AC colConstraints);
73
public MigLayout(LC layoutConstraints, AC colConstraints, AC rowConstraints);
74
}
75
```
76
77
[Layout Management](./layout-management.md)
78
79
### Component Integration
80
81
Component wrapper system providing seamless integration between Swing components and the MiGLayout constraint system.
82
83
```java { .api }
84
public class SwingComponentWrapper implements ComponentWrapper {
85
public SwingComponentWrapper(Component c);
86
public Object getComponent();
87
public int getBaseline(int width, int height);
88
public float getPixelUnitFactor(boolean isHor);
89
}
90
91
public final class SwingContainerWrapper extends SwingComponentWrapper implements ContainerWrapper {
92
public SwingContainerWrapper(Container c);
93
public ComponentWrapper[] getComponents();
94
public int getComponentCount();
95
}
96
```
97
98
[Component Integration](./component-integration.md)
99
100
### Constraint Configuration
101
102
Structured constraint objects for programmatic layout configuration, providing type-safe alternatives to string-based constraints.
103
104
```java { .api }
105
// Layout constraints (from core module)
106
public class LC implements Externalizable {
107
public LC();
108
// Configuration methods for layout-level settings
109
}
110
111
// Axis constraints for columns/rows (from core module)
112
public class AC implements Externalizable {
113
public AC();
114
// Configuration methods for column/row constraints
115
}
116
117
// Component constraints (from core module)
118
public class CC implements Externalizable {
119
public CC();
120
// Configuration methods for individual component constraints
121
}
122
```
123
124
[Constraint Configuration](./constraint-configuration.md)
125
126
## Types
127
128
```java { .api }
129
// Component type constants
130
interface ComponentWrapper {
131
int TYPE_UNSET = -1;
132
int TYPE_UNKNOWN = 0;
133
int TYPE_CONTAINER = 1;
134
int TYPE_LABEL = 2;
135
int TYPE_TEXT_FIELD = 3;
136
int TYPE_TEXT_AREA = 4;
137
int TYPE_BUTTON = 5;
138
int TYPE_CHECK_BOX = 6;
139
int TYPE_COMBO_BOX = 7;
140
int TYPE_PANEL = 8;
141
int TYPE_LIST = 9;
142
int TYPE_TABLE = 10;
143
int TYPE_SEPARATOR = 11;
144
int TYPE_SPINNER = 12;
145
int TYPE_TABBED_PANE = 13;
146
int TYPE_PROGRESS_BAR = 14;
147
int TYPE_SLIDER = 15;
148
int TYPE_SCROLL_PANE = 16;
149
int TYPE_SCROLL_BAR = 17;
150
int TYPE_IMAGE = 18;
151
}
152
153
// Layout callback interface (from core module)
154
interface LayoutCallback {
155
// Callback methods for layout events
156
}
157
```