Java JButton Settings ScaleButtonIcon(JToggleButton btn, int width, int height, int fontsize)

Here you can find the source of ScaleButtonIcon(JToggleButton btn, int width, int height, int fontsize)

Description

Scale Button Icon

License

Apache License

Declaration

public static void ScaleButtonIcon(JToggleButton 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   w  ww. ja  va2  s.  c o m

        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. removeButtonBorder(AbstractButton button)
  2. removeCloseButton(Component comp)
  3. removeListeners(AbstractButton button)
  4. scaleAllAbstractButtonIconsOf(Container container, int size)
  5. scaleButtonIcon(Icon icon, int size)
  6. SelectedOptionButton(Container container)
  7. selectFile(final int openMode, final String title, final String buttonText, final String lastDirectoryUsed, final Component parent, final String suffix, final String description)
  8. selectWithoutNotifyingListeners(AbstractButton ab, boolean selected)
  9. setAction(AbstractButton btn, ActionListener listener, String actionCommand)