Here you can find the source of setMargin(final JComponent component, final int eachSide)
public static void setMargin(final JComponent component, final int eachSide)
//package com.java2s; // Licensed under the MIT license. See License.txt in the project root. import javax.swing.JComponent; import javax.swing.border.Border; import javax.swing.border.CompoundBorder; import javax.swing.border.EmptyBorder; import java.awt.Insets; public class Main { public static void setMargin(final JComponent component, final int eachSide) { final Insets insets = new Insets(eachSide, eachSide, eachSide, eachSide); setMargin(component, insets);/*from ww w . j a va2 s. c o m*/ } public static void setMargin(final JComponent component, final Insets newMargin) { final Border currentBorder = component.getBorder(); final Border empty = new EmptyBorder(newMargin.top, newMargin.left, newMargin.bottom, newMargin.right); if (currentBorder == null || currentBorder instanceof EmptyBorder) { component.setBorder(empty); } else if (currentBorder instanceof CompoundBorder) { final CompoundBorder current = (CompoundBorder) currentBorder; final Border insideBorder = current.getInsideBorder(); component.setBorder(new CompoundBorder(empty, insideBorder)); } else { component.setBorder(new CompoundBorder(empty, currentBorder)); } } }