Example usage for javax.swing JButton getGraphics

List of usage examples for javax.swing JButton getGraphics

Introduction

In this page you can find the example usage for javax.swing JButton getGraphics.

Prototype

@BeanProperty(bound = false)
public Graphics getGraphics() 

Source Link

Document

Returns this component's graphics context, which lets you draw on a component.

Usage

From source file:Main.java

public static void setJButtonSizesTheSame(final JButton[] btns) {
    if (btns == null) {
        throw new IllegalArgumentException();
    }//from w  w  w  . jav a  2 s  . com

    final Dimension maxSize = new Dimension(0, 0);
    for (int i = 0; i < btns.length; ++i) {
        final JButton btn = btns[i];
        final FontMetrics fm = btn.getFontMetrics(btn.getFont());
        final Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics());
        final int boundsHeight = (int) bounds.getHeight();
        final int boundsWidth = (int) bounds.getWidth();
        maxSize.width = boundsWidth > maxSize.width ? boundsWidth : maxSize.width;
        maxSize.height = boundsHeight > maxSize.height ? boundsHeight : maxSize.height;
    }

    final Insets insets = btns[0].getInsets();
    maxSize.width += insets.left + insets.right;
    maxSize.height += insets.top + insets.bottom;

    for (int i = 0; i < btns.length; ++i) {
        final JButton btn = btns[i];
        btn.setPreferredSize(maxSize);
    }

    initComponentHeight(btns);
}

From source file:GUIUtils.java

/**
 * Change the sizes of all the passed buttons to be the size of the largest
 * one.//w  ww  .  j  av a2s  .com
 * 
 * @param btns
 *          Array of buttons to eb resized.
 * 
 * @throws IllegalArgumentException
 *           If <TT>btns</TT> is <TT>null</TT>.
 */
public static void setJButtonSizesTheSame(JButton[] btns) {
    if (btns == null) {
        throw new IllegalArgumentException("null JButton[] passed");
    }

    // Get the largest width and height
    final Dimension maxSize = new Dimension(0, 0);
    for (int i = 0; i < btns.length; ++i) {
        final JButton btn = btns[i];
        final FontMetrics fm = btn.getFontMetrics(btn.getFont());
        Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics());
        int boundsHeight = (int) bounds.getHeight();
        int boundsWidth = (int) bounds.getWidth();
        maxSize.width = boundsWidth > maxSize.width ? boundsWidth : maxSize.width;
        maxSize.height = boundsHeight > maxSize.height ? boundsHeight : maxSize.height;
    }

    Insets insets = btns[0].getInsets();
    maxSize.width += insets.left + insets.right;
    maxSize.height += insets.top + insets.bottom;

    for (int i = 0; i < btns.length; ++i) {
        JButton btn = btns[i];
        btn.setPreferredSize(maxSize);
    }
}