Initializes a default margin in provided directions. - Java Swing

Java examples for Swing:Border

Description

Initializes a default margin in provided directions.

Demo Code


//package com.java2s;

import javax.swing.BorderFactory;

import javax.swing.SwingConstants;

import javax.swing.border.Border;

public class Main {
    /**//from  w w  w .  j  a  v  a  2 s  . c o m
     * Default program-wide margin.
     */
    private static final int DEFAULT_MARGIN = 8;

    /**
     * Initializes a default margin in provided directions.
     * <p>
     * Directions provided can be one of the TOP, LEFT, DOWN and BOTTOM
     * constants from the {@link SwingConstants} interface.
     * 
     * @param directions Array of directions where the border is to be created.
     * @return Empty border to be used as margin; border width defined as
     * {@link #DEFAULT_MARGIN}.
     */
    public static Border defaultMargin(int... directions) {
        int[] borders = new int[4];
        if (directions == null)
            return BorderFactory.createEmptyBorder();

        for (int i = 0; i < directions.length; i++) {
            if (directions[i] == SwingConstants.TOP)
                borders[0] = DEFAULT_MARGIN;
            if (directions[i] == SwingConstants.LEFT)
                borders[1] = DEFAULT_MARGIN;
            if (directions[i] == SwingConstants.BOTTOM)
                borders[2] = DEFAULT_MARGIN;
            if (directions[i] == SwingConstants.RIGHT)
                borders[3] = DEFAULT_MARGIN;
        }

        return BorderFactory.createEmptyBorder(borders[0], borders[1],
                borders[2], borders[3]);
    }
}

Related Tutorials