Java JButton Settings setHorizontalMargin(AbstractButton button, int hMargin)

Here you can find the source of setHorizontalMargin(AbstractButton button, int hMargin)

Description

Sets the left and right margin of an AbstractButton in a L&F-independent manner (some L&Fs set this value in the margin, while others ignore this property and use the border instead).

License

Open Source License

Declaration

public static void setHorizontalMargin(AbstractButton button, int hMargin) 

Method Source Code


//package com.java2s;
/*/*from  w  ww . ja v a 2  s .c  om*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

import java.awt.*;

import javax.swing.*;
import javax.swing.border.Border;

public class Main {
    /**
     * Sets the left and right margin of an {@code AbstractButton} in a
     * L&F-independent manner (some L&Fs set this value in the margin,
     * while others ignore this property and use the border instead).
     */
    public static void setHorizontalMargin(AbstractButton button, int hMargin) {
        Insets margin = button.getMargin();
        margin.left = margin.right = hMargin;

        Border empty = BorderFactory.createEmptyBorder(margin.top, margin.left, margin.bottom, margin.right);

        Border outer = button.getBorder();
        Border compound = BorderFactory.createCompoundBorder(outer, empty);

        button.setMargin(new Insets(0, 0, 0, 0));
        button.setBorder(compound);
    }

    public static Border createCompoundBorder(Border... borders) {
        Border border = null;
        for (int i = 0; i < borders.length; i++) {
            border = i == 0 ? borders[i] : BorderFactory.createCompoundBorder(border, borders[i]);
        }
        return border;
    }
}

Related

  1. setButtonInsetsRecursive(Insets insets, Container container)
  2. setButtonSelected(ButtonGroup buttonGroup, int selected)
  3. setButtonStyle(AbstractButton btn)
  4. setButtonText(ButtonGroup buttonGroup, List texts)
  5. setHelpIDString(javax.swing.AbstractButton btn, String id)
  6. setImageIcon(AbstractButton abstractButton, URL url)
  7. setMnemonic(AbstractButton actionComponent)
  8. setPreferredWidth(JComponent button, int minWidth)
  9. setRadioButtonSelected(Window window, String buttonText)