Java JComponent Margin setMargin(final JComponent component, final int eachSide)

Here you can find the source of setMargin(final JComponent component, final int eachSide)

Description

set Margin

License

Open Source License

Declaration

public static void setMargin(final JComponent component, final int eachSide) 

Method Source Code


//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));
        }
    }
}

Related

  1. addMargin(JComponent component, int margin)