Java JButton Size ScaleButtonIcon(javax.swing.JButton btn, int width, int height, int fontsize)

Here you can find the source of ScaleButtonIcon(javax.swing.JButton btn, int width, int height, int fontsize)

Description

Scale Button Icon

License

Apache License

Declaration

public static void ScaleButtonIcon(javax.swing.JButton btn, int width, int height, int fontsize) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Font;
import java.awt.FontMetrics;

import java.awt.Image;

import javax.swing.JButton;

import javax.swing.JToggleButton;

public class Main {
    public static void ScaleButtonIcon(javax.swing.JButton btn, int width, int height, int fontsize) {
        btn.setMargin(null);/*from ww  w .  j a  v a 2  s  . c om*/

        int iconWidth = width;
        int iconHeigth = height;

        if (btn.getText() != null && !btn.getText().isEmpty()) {
            // String text = btn.getText();
            Font font = btn.getFont();
            btn.setFont(new Font(font.getName(), font.getStyle(), fontsize));

            FontMetrics fm = btn.getFontMetrics(font);
            // newWidth += fm.stringWidth(text);
            btn.setHorizontalTextPosition(JButton.CENTER);
            btn.setVerticalTextPosition(JButton.BOTTOM);
            iconWidth -= fm.getHeight();
            iconHeigth -= fm.getHeight();
        }

        if (btn.getIcon() != null && javax.swing.ImageIcon.class.isAssignableFrom(btn.getIcon().getClass())) {
            javax.swing.ImageIcon icon = javax.swing.ImageIcon.class.cast(btn.getIcon());
            double radio = icon.getIconWidth() / icon.getIconWidth();
            Image img = icon.getImage().getScaledInstance(radio > 1 ? iconWidth : -1, radio > 1 ? -1 : iconHeigth,
                    Image.SCALE_SMOOTH);
            btn.setIcon(new javax.swing.ImageIcon(img));
        }

        btn.setSize(width, height);
        // btn.setMinimumSize(new Dimension(newWidth, newHeight));
        // btn.setPreferredSize(new Dimension(width, newHeight));
    }

    public static void ScaleButtonIcon(javax.swing.JButton btn, int width, int height, int align, int fontsize) {
        int newWidth = width;
        int newHeight = height;
        btn.setMargin(null);
        if (btn.getIcon() != null && javax.swing.ImageIcon.class.isAssignableFrom(btn.getIcon().getClass())) {
            javax.swing.ImageIcon icon = javax.swing.ImageIcon.class.cast(btn.getIcon());
            double radio = icon.getIconWidth() / icon.getIconWidth();
            Image img = icon.getImage().getScaledInstance(radio > 1 ? width : -1, radio > 1 ? -1 : height,
                    Image.SCALE_SMOOTH);
            btn.setIcon(new javax.swing.ImageIcon(img));
        }

        if (btn.getText() != null && !btn.getText().isEmpty()) {
            String text = btn.getText();
            Font font = btn.getFont();
            btn.setFont(new Font(font.getName(), font.getStyle(), fontsize));
            FontMetrics fm = btn.getFontMetrics(font);
            // newWidth += fm.stringWidth(text);
            btn.setHorizontalTextPosition(align);
            btn.setVerticalTextPosition(JButton.BOTTOM);
            newWidth += fm.getHeight();
            newHeight += fm.getHeight();
        }

        btn.setSize(newWidth, newHeight);
        // btn.setMinimumSize(new Dimension(newWidth, newHeight));
        // btn.setPreferredSize(new Dimension(newWidth, newHeight));
    }

    public static void ScaleButtonIcon(JToggleButton btn, int width, int height, int fontsize) {
        btn.setMargin(null);

        int iconWidth = width;
        int iconHeigth = height;

        if (btn.getText() != null && !btn.getText().isEmpty()) {
            // String text = btn.getText();
            Font font = btn.getFont();
            btn.setFont(new Font(font.getName(), font.getStyle(), fontsize));

            FontMetrics fm = btn.getFontMetrics(font);
            // newWidth += fm.stringWidth(text);
            btn.setHorizontalTextPosition(JButton.CENTER);
            btn.setVerticalTextPosition(JButton.BOTTOM);
            iconWidth -= fm.getHeight();
            iconHeigth -= fm.getHeight();
        }

        if (btn.getIcon() != null && javax.swing.ImageIcon.class.isAssignableFrom(btn.getIcon().getClass())) {
            javax.swing.ImageIcon icon = javax.swing.ImageIcon.class.cast(btn.getIcon());
            double radio = icon.getIconWidth() / icon.getIconWidth();
            Image img = icon.getImage().getScaledInstance(radio > 1 ? iconWidth : -1, radio > 1 ? -1 : iconHeigth,
                    Image.SCALE_SMOOTH);
            btn.setIcon(new javax.swing.ImageIcon(img));
        }
        if (btn.getSelectedIcon() != null
                && javax.swing.ImageIcon.class.isAssignableFrom(btn.getSelectedIcon().getClass())) {
            javax.swing.ImageIcon icon = javax.swing.ImageIcon.class.cast(btn.getSelectedIcon());
            double radio = icon.getIconWidth() / icon.getIconWidth();
            Image img = icon.getImage().getScaledInstance(radio > 1 ? iconWidth : -1, radio > 1 ? -1 : iconHeigth,
                    Image.SCALE_SMOOTH);
            btn.setSelectedIcon(new javax.swing.ImageIcon(img));
        }

        btn.setSize(width, height);

        // int newWidth = width;
        // int newHeight = height;
        // btn.setMargin(null);
        // if (btn.getIcon() != null &&
        // javax.swing.ImageIcon.class.isAssignableFrom(btn.getIcon().getClass()))
        // {
        // javax.swing.ImageIcon icon =
        // javax.swing.ImageIcon.class.cast(btn.getIcon());
        // double radio = icon.getIconWidth() / icon.getIconWidth();
        // Image img = icon.getImage().getScaledInstance(radio > 1 ? width : -1,
        // radio > 1 ? -1 : height,
        // Image.SCALE_SMOOTH);
        // btn.setIcon(new javax.swing.ImageIcon(img));
        // }
        // if (btn.getSelectedIcon() != null
        // &&
        // javax.swing.ImageIcon.class.isAssignableFrom(btn.getSelectedIcon().getClass()))
        // {
        // javax.swing.ImageIcon selectionicon =
        // javax.swing.ImageIcon.class.cast(btn.getSelectedIcon());
        // double radio2 = selectionicon.getIconWidth() /
        // selectionicon.getIconWidth();
        // Image img2 = selectionicon.getImage().getScaledInstance(radio2 > 1 ?
        // width : -1, radio2 > 1 ? -1 : height,
        // Image.SCALE_SMOOTH);
        // btn.setSelectedIcon(new javax.swing.ImageIcon(img2));
        // }
        //
        // if (btn.getText() != null && !btn.getText().isEmpty()) {
        // String text = btn.getText();
        // Font font = btn.getFont();
        // btn.setFont(new Font(font.getName(), font.getStyle(), fontsize));
        // FontMetrics fm = btn.getFontMetrics(font);
        // // newWidth += fm.stringWidth(text);
        // btn.setHorizontalTextPosition(JButton.CENTER);
        // btn.setVerticalTextPosition(JButton.BOTTOM);
        // newWidth += fm.getHeight();
        // newHeight += fm.getHeight();
        // }
        //
        // btn.setSize(newWidth, newHeight);
        // btn.setMinimumSize(new Dimension(newWidth, newHeight));
        // btn.setPreferredSize(new Dimension(width, newHeight));
    }
}

Related

  1. adjustSize(JButton button)
  2. equalizeSize(JButton[] aButtons)
  3. makeButtonsSameSize(JButton[] buttons)
  4. setButtonSize(JButton button, Dimension size)
  5. setButtonSize(JButton button, int width, int height)
  6. setJButtonSizesTheSame(final JButton[] btns)
  7. setJButtonSizesTheSame(JButton[] btns)