Java JButton Settings fixButtonUI(AbstractButton button)

Here you can find the source of fixButtonUI(AbstractButton button)

Description

Ensures that focus will be really painted if button is focused and fixes using custom border for JDK 1.5 & XP LaF

License

Open Source License

Declaration

public static void fixButtonUI(AbstractButton button) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics;

import java.awt.Rectangle;

import javax.swing.AbstractButton;

import javax.swing.plaf.basic.BasicButtonListener;

public class Main {
    /**//from  ww  w .  j  a  va2 s .c  om
     * Ensures that focus will be really painted if button is focused and fixes
     * using custom border for JDK 1.5 & XP LaF
     */
    public static void fixButtonUI(AbstractButton button) {
        // JButton
        if (button.getUI() instanceof com.sun.java.swing.plaf.windows.WindowsButtonUI) {
            button.setUI(new com.sun.java.swing.plaf.windows.WindowsButtonUI() {
                protected BasicButtonListener createButtonListener(AbstractButton b) {
                    return new BasicButtonListener(b); // Fix for  Issue 71546
                }

                protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect,
                        Rectangle iconRect) {
                    int width = b.getWidth();
                    int height = b.getHeight();
                    g.setColor(getFocusColor());
                    javax.swing.plaf.basic.BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY,
                            width - dashedRectGapWidth, height - dashedRectGapHeight);
                }
            });
        } // JToggleButton
        else if (button.getUI() instanceof com.sun.java.swing.plaf.windows.WindowsToggleButtonUI) {
            button.setUI(new com.sun.java.swing.plaf.windows.WindowsToggleButtonUI() {
                protected BasicButtonListener createButtonListener(AbstractButton b) {
                    return new BasicButtonListener(b); // Fix for  Issue 71546
                }

                protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect,
                        Rectangle iconRect) {
                    int width = b.getWidth();
                    int height = b.getHeight();
                    g.setColor(getFocusColor());
                    javax.swing.plaf.basic.BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY,
                            width - dashedRectGapWidth, height - dashedRectGapHeight);
                }
            });
        }
    }
}

Related

  1. equalizeButtonSizes(JPanel jPanelButtons)
  2. filterMnemonic(final String s, final AbstractButton b)
  3. findButton(Container container, String text)
  4. findButtonComponents(Container container, String label)
  5. findDescendantButtonWithText(Container root, String desiredText)
  6. formatButtonFromAction(Action a)
  7. getAbstractButton(Container owner, String text)
  8. getAllButtons(Component topComponent)
  9. getbtnButton(String iconpath, String rolloverIconpath, String pressIconpath)