Java JButton Settings getTextXOffset(AbstractButton c)

Here you can find the source of getTextXOffset(AbstractButton c)

Description

get Text X Offset

License

Open Source License

Declaration

public static int getTextXOffset(AbstractButton c) 

Method Source Code


//package com.java2s;
/*/*from   w  w w  .  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 {
    public static int getTextXOffset(AbstractButton c) {
        int xoffset = c.getPreferredSize().width;

        Border b = c.getBorder();
        if (b != null) {
            xoffset -= b.getBorderInsets(c).right;
        }

        //   Insets m = c.getMargin();
        //   if (m != null) {
        //       xoffset -= m.right;
        //   }

        String text = c.getText();
        if (text != null) {
            xoffset -= c.getFontMetrics(c.getFont()).stringWidth(text);
        }

        return xoffset;
    }

    /**
     * Gets the preferred size of the given {@code Window}.  If the given {@code
     * Window} is a {@code JFrame} or a {@code JDialog}, and its glass pane is
     * visible, the glass pane's preferred size will be accounted for as well
     * ({@code Window.getPreferredSize} does not take the glass pane into
     * consideration).
     */
    public static Dimension getPreferredSize(Window window) {
        Dimension pSize = window.getPreferredSize();

        Component glass = null;
        Component content = null;

        if (window instanceof JFrame) {
            JFrame frame = (JFrame) window;
            glass = frame.getGlassPane();
            content = frame.getContentPane();
        } else if (window instanceof JDialog) {
            JDialog dialog = (JDialog) window;
            glass = dialog.getGlassPane();
            content = dialog.getContentPane();
        }

        if (glass != null && glass.isVisible()) {
            Dimension cpSize = content.getPreferredSize();
            content.setPreferredSize(glass.getPreferredSize());

            Dimension newPSize = window.getPreferredSize();

            pSize.width = Math.max(pSize.width, newPSize.width);
            pSize.height = Math.max(pSize.height, newPSize.height);

            // Restore previous preferred size, if any
            content.setPreferredSize(cpSize);
        }

        return pSize;
    }
}

Related

  1. getRadioButtonBorder(int fontSize, boolean ltr)
  2. getSelectedButton(ButtonGroup group)
  3. getSelectedButtonText(ButtonGroup buttonGroup)
  4. getSelectedButtonText(ButtonGroup buttonGroup)
  5. getSoftToggleButton(String string)
  6. getTogglableIconButton(ImageIcon icon)
  7. getWindowFileChooserOKButton(JFileChooser chooser)
  8. groupButtons(AbstractButton... buttons)
  9. groupButtons(final Container container, final boolean recursive)