MiGLayout Swing implementation providing constraint-based layout management for Java Swing applications
—
Core layout manager implementation providing constraint-based positioning and sizing for Swing containers with support for string-based and programmatic constraint definition.
Primary layout manager implementing LayoutManager2 for Swing container integration.
/**
* A very flexible layout manager for Swing containers.
* Implements LayoutManager2 and Externalizable for full Swing integration.
*/
public class MigLayout implements LayoutManager2, Externalizable {
/** Constructor with no constraints. */
public MigLayout();
/**
* Constructor with layout constraints only.
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as "".
*/
public MigLayout(String layoutConstraints);
/**
* Constructor with layout and column constraints.
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as "".
* @param colConstraints The constraints for the columns in the grid. null will be treated as "".
*/
public MigLayout(String layoutConstraints, String colConstraints);
/**
* Constructor with all string constraints.
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as "".
* @param colConstraints The constraints for the columns in the grid. null will be treated as "".
* @param rowConstraints The constraints for the rows in the grid. null will be treated as "".
*/
public MigLayout(String layoutConstraints, String colConstraints, String rowConstraints);
/**
* Constructor with LC object.
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as an empty constraint.
*/
public MigLayout(LC layoutConstraints);
/**
* Constructor with LC and AC objects.
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as an empty constraint.
* @param colConstraints The constraints for the columns in the grid. null will be treated as an empty constraint.
*/
public MigLayout(LC layoutConstraints, AC colConstraints);
/**
* Constructor with all constraint objects.
* @param layoutConstraints The constraints that concern the whole layout. null will be treated as an empty constraint.
* @param colConstraints The constraints for the columns in the grid. null will be treated as an empty constraint.
* @param rowConstraints The constraints for the rows in the grid. null will be treated as an empty constraint.
*/
public MigLayout(LC layoutConstraints, AC colConstraints, AC rowConstraints);
}Usage Examples:
import net.miginfocom.swing.MigLayout;
import net.miginfocom.layout.LC;
import net.miginfocom.layout.AC;
import javax.swing.*;
// String-based approach
MigLayout stringLayout = new MigLayout(
"wrap 3, insets 10", // Layout constraints
"[grow][100:100:150][grow]", // Column constraints
"[]10[]" // Row constraints
);
// Programmatic approach
LC layoutConstraints = new LC()
.wrapAfter(3)
.insets("10");
AC colConstraints = new AC()
.grow()
.size("100:100:150")
.grow();
AC rowConstraints = new AC()
.gap("10");
MigLayout programmaticLayout = new MigLayout(layoutConstraints, colConstraints, rowConstraints);
// Apply to container
JPanel panel = new JPanel(stringLayout);Methods for getting and setting layout, column, and row constraints at runtime.
/**
* Returns layout constraints either as a String or LC depending what was sent in to the constructor.
* @return The layout constraints either as a String or LC. Never null.
*/
public Object getLayoutConstraints();
/**
* Sets the layout constraints for the layout manager instance.
* @param constr The layout constraints as a String or LC representation. null is converted to "" for storage.
* @throws RuntimeException if the constraint was not valid.
*/
public void setLayoutConstraints(Object constr);
/**
* Returns the column layout constraints either as a String or AC.
* @return The column constraints either as a String or AC. Never null.
*/
public Object getColumnConstraints();
/**
* Sets the column layout constraints for the layout manager instance.
* @param constr The column layout constraints as a String or AC representation. null is converted to "" for storage.
* @throws RuntimeException if the constraint was not valid.
*/
public void setColumnConstraints(Object constr);
/**
* Returns the row layout constraints either as a String or AC.
* @return The row constraints either as a String or AC. Never null.
*/
public Object getRowConstraints();
/**
* Sets the row layout constraints for the layout manager instance.
* @param constr The row layout constraints as a String or AC representation. null is converted to "" for storage.
* @throws RuntimeException if the constraint was not valid.
*/
public void setRowConstraints(Object constr);Methods for managing individual component constraints within the layout.
/**
* Returns a shallow copy of the constraints map.
* @return A shallow copy of the constraints map. Never null.
*/
public Map<Component, Object> getConstraintMap();
/**
* Sets the constraints map.
* @param map The map. Will be copied.
*/
public void setConstraintMap(Map<Component, Object> map);
/**
* Returns the component constraints as a String representation.
* @param comp The component to return the constraints for.
* @return The component constraints as a String representation or null if the component is not registered.
* The returned values is either a String or a CC depending on what constraint was sent in when the component was added.
*/
public Object getComponentConstraints(Component comp);
/**
* Sets the component constraint for the component that already must be handled by this layout manager.
* @param comp The component to set the constraints for.
* @param constr The component constraints as a String or CC. null is ok.
* @throws RuntimeException if the constraint was not valid.
* @throws IllegalArgumentException If the component is not handling the component.
*/
public void setComponentConstraints(Component comp, Object constr);
/**
* Returns if this layout manager is currently managing this component.
* @param c The component to check. If null then false will be returned.
* @return If this layout manager is currently managing this component.
*/
public boolean isManagingComponent(Component c);Methods for adding and removing layout callback functions that are called at different stages of the layout cycle.
/**
* Adds the callback function that will be called at different stages of the layout cycle.
* @param callback The callback. Not null.
*/
public void addLayoutCallback(LayoutCallback callback);
/**
* Removes the callback if it exists.
* @param callback The callback. May be null.
*/
public void removeLayoutCallback(LayoutCallback callback);Standard Swing layout manager interface implementation for container integration.
/**
* Performs layout of the container.
* @param parent The container to lay out.
*/
public void layoutContainer(Container parent);
/**
* Returns the minimum layout size.
* @param parent The container to calculate minimum size for.
* @return The minimum dimension.
*/
public Dimension minimumLayoutSize(Container parent);
/**
* Returns the preferred layout size.
* @param parent The container to calculate preferred size for.
* @return The preferred dimension.
*/
public Dimension preferredLayoutSize(Container parent);
/**
* Returns the maximum layout size.
* @param parent The container to calculate maximum size for.
* @return The maximum dimension (Integer.MAX_VALUE for both width and height).
*/
public Dimension maximumLayoutSize(Container parent);
/**
* Returns the alignment along the X axis.
* @param parent The container.
* @return The X axis alignment.
*/
public float getLayoutAlignmentX(Container parent);
/**
* Returns the alignment along the Y axis.
* @param parent The container.
* @return The Y axis alignment.
*/
public float getLayoutAlignmentY(Container parent);
/**
* Adds a component to the layout with string constraint.
* @param s The constraint string.
* @param comp The component to add.
*/
public void addLayoutComponent(String s, Component comp);
/**
* Adds a component to the layout with object constraint.
* @param comp The component to add.
* @param constraints The constraint object (String or CC).
*/
public void addLayoutComponent(Component comp, Object constraints);
/**
* Removes a component from the layout.
* @param comp The component to remove.
*/
public void removeLayoutComponent(Component comp);
/**
* Invalidates the layout cache.
* @param target The container target.
*/
public void invalidateLayout(Container target);Static utility methods for component hierarchy navigation.
/**
* Finds parent component of specific type in the component hierarchy.
* @param clazz The class type to find.
* @param comp The starting component.
* @return The parent component of the specified type, or null if not found.
*/
public static <E> E findType(Class<E> clazz, Component comp);Methods for object serialization and deserialization support.
/**
* Reads the object state from the input stream for deserialization.
* @param in The ObjectInput to read from
* @throws IOException if I/O error occurs
* @throws ClassNotFoundException if class cannot be found during deserialization
*/
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;
/**
* Writes the object state to the output stream for serialization.
* @param out The ObjectOutput to write to
* @throws IOException if I/O error occurs
*/
public void writeExternal(ObjectOutput out) throws IOException;Usage Examples:
import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import java.awt.*;
// Dynamic constraint modification
MigLayout layout = new MigLayout("wrap 2");
JPanel panel = new JPanel(layout);
JButton button = new JButton("Click me");
panel.add(button, "growx");
// Later modify constraints
layout.setComponentConstraints(button, "growx, span 2");
// Check if component is managed
if (layout.isManagingComponent(button)) {
// Component is managed by this layout
Object constraints = layout.getComponentConstraints(button);
}
// Find parent window
Window parentWindow = MigLayout.findType(Window.class, button);
// Serialization support
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("layout.ser"))) {
layout.writeExternal(out);
}
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("layout.ser"))) {
MigLayout restoredLayout = new MigLayout();
restoredLayout.readExternal(in);
}Install with Tessl CLI
npx tessl i tessl/maven-com-miglayout--miglayout-swing