Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.Container; import javax.swing.SpringLayout; public class Main { /** * Set the anchors of each edge of a component to each edge of the passed in parent.</br> * The parent must have a {@link javax.swing.SpringLayout} as its {@link java.awt.LayoutManager}.</br> * </br> * Pass in -1 for any of the anchors and that edge will not be attached</br> * Note that this method will remove all pre-existing anchors attached to item. * * @param top */ public static void setSpringAnchors(int top, int right, int bottom, int left, Component comp, Container parent) { if (parent == null || comp == null) { throw new IllegalArgumentException("Parent and Component cannot be null"); } if (!(parent.getLayout() instanceof SpringLayout)) { throw new IllegalArgumentException("Parent container does not have SpringLayout as its LayoutManager"); } SpringLayout layout = (SpringLayout) parent.getLayout(); layout.removeLayoutComponent(comp); if (top >= 0) layout.putConstraint(SpringLayout.NORTH, parent, top, SpringLayout.NORTH, comp); if (right >= 0) layout.putConstraint(SpringLayout.EAST, parent, right, SpringLayout.EAST, comp); if (bottom >= 0) layout.putConstraint(SpringLayout.SOUTH, parent, bottom, SpringLayout.SOUTH, comp); if (left >= 0) layout.putConstraint(SpringLayout.WEST, parent, left, SpringLayout.WEST, comp); } }