开发者

set default disabled font color for <html> JRadioButton

I have a JRadioButton with some text, containing html code. When I set it to disabled, color of text doesn't changed to gray or something else. How can I set it to default disabled component text color? I can set text color directly in text like:

<html><font color=someColor>...

but how can I get default color for disabled component text? Also I've tried to override paint method and use something like this:

Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.35f));
super.paintComponent(g2);
g2.dispose();

but I didn't got expected result - it became gray, but not identical to default disabled component text color.

So, the solution may be to get disabled color from UIManager.getColor("ComboBox.disabledForeground"); cause this property available in all Os. And here is the code:

import javax.swing.*;
import java.awt.*;

public class HTMLJRadio extends J开发者_开发技巧RadioButton {

static String prefixEnabled = "<html><body style='color: black;'>";
String text;
String prefixDisabled;

HTMLJRadio(String text) {
    super(prefixEnabled + text);
    this.text = text;
    Color c = UIManager.getColor("ComboBox.disabledForeground");
    String color = Integer.toHexString(c.getRed()) +
            Integer.toHexString(c.getGreen()) +
            Integer.toHexString(c.getBlue());
    prefixDisabled = "<html><body style='color: #" + color + ";'>";
}

public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    if (enabled) {
        setText(prefixEnabled + text);
    } else {
        setText(prefixDisabled + text);
    }
}

public static void showButtons() {
    String htmlText = "<h1>Laf</h1><p>Ha Ha!";
    JPanel p = new JPanel(new GridLayout(0, 1, 3, 3));
    HTMLJRadio button1 = new HTMLJRadio(htmlText);
    p.add(button1);
    HTMLJRadio button2 = new HTMLJRadio(htmlText);
    button2.setEnabled(false);
    p.add(button2);

    JRadioButton button3 = new JRadioButton("Non html disabled");
    button3.setEnabled(false);
    p.add(button3);
    JOptionPane.showMessageDialog(null, p);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
            }
            showButtons();
        }
    });
}
}

On ubuntu it looks like:

set default disabled font color for <html> JRadioButton

So the disabled radio button with html inside looks very similar to disabled radio button without html inside. Also you can use solution from answer, with some magic with images.


Edit: (by A.T.) Previously this question was marked as 'correct'. By mutual agreement, the OP withdrew the correct marking, since the offered answer does not cover the subtleties shown in the screen shot. It is particularly the 'embossed' effect that is around the text in the lower button, that sets it apart from the disabled HTML formatted button.

Further suggestions as to how to achieve that effect would be appreciated (by both of us).


From How to Use HTML in Swing Components:

...Note also that when a button is disabled, its HTML text unfortunately remains black, instead of becoming gray. (Refer to bug #4783068 to see if this situation changes.)


Maybe you can get by starting with something like this.

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

class HTMLButton extends JButton {

    static String prefixEnabled = "<html><body style='color: black;'>";
    String text;
    String prefixDisabled;

    HTMLButton(String text) {
        super(prefixEnabled + text);
        this.text = text;
        Color c = determineDisabledColorByWitchCraft();
            //UIManager.getColor("Button.disabledText");
        String color =
            Integer.toHexString(c.getRed()) +
            Integer.toHexString(c.getGreen()) +
            Integer.toHexString(c.getBlue());
        prefixDisabled = "<html><body style='color: #" + color + ";'>";
    }

    private static String getHex(int n) {
        return Integer.toHexString(n);
    }

    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (enabled) {
            setText(prefixEnabled + text);
        } else {
            setText(prefixDisabled + text);
        }
    }

    public static Color determineDisabledColorByWitchCraft() {
        // this is little 'block' character..
        JButton b = new JButton(String.valueOf('\u2586'));
        b.setSize(b.getPreferredSize());
        b.setEnabled(false);
        BufferedImage biDisabled = new BufferedImage(
            b.getWidth(),
            b.getHeight(),
            BufferedImage.TYPE_INT_RGB);
        Graphics g2 = biDisabled.getGraphics();
        b.paint(g2);

        // get the middle pixel..
        int x = b.getWidth()/2;
        int y = b.getHeight()/2;

        return new Color(biDisabled.getRGB(x,y));
    }

    public static void showButtons() {
        String htmlText = "<h1>Laf</h1><p>Ha Ha!";
        JPanel p = new JPanel(new GridLayout(0,1,3,3));
        HTMLButton button1 = new HTMLButton(htmlText);
        p.add(button1);
        HTMLButton button2 = new HTMLButton(htmlText);
        button2.setEnabled(false);
        p.add(button2);

        JButton button3 = new JButton("Hi there!");
        button3.setEnabled(false);
        p.add(button3);
        JOptionPane.showMessageDialog(null, p);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                showButtons();

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch(Exception e) {
                }
                showButtons();
            }
        });
    }
}

Screenshot of disabled button using default disabled color

set default disabled font color for <html> JRadioButton


Update

(Failed) attempt to get the effect using CSS positioning. Just thought I'd report the latest failure, to save other people the trouble of wondering whether it might suffice.

This HTML:

<html>
<head>
<style type='text/css'>
body {
    font-size: 16px;
}
.main {
    position: fixed;
    top: -16px;
    left: 0px;
    color: black;
}
.emboss {
    position: fixed;
    top: 0px;
    left: 1px;
    color: red;
}
</style>
</head>
<body>

<p class='emboss'><b>The quick brown fox jumped over the lazy dog.</b>
<p class='main'><b>The quick brown fox jumped over the lazy dog.</b>

</body>
</html>

Which renders in a browser (e.g. FF) much like this:

set default disabled font color for <html> JRadioButton

..renders in JEditorPane like this:

set default disabled font color for <html> JRadioButton

:-(


I think I've been working for it for 3 hours (no school :D). And the result is good! Here is a screenshot of it in Ubuntu:

set default disabled font color for <html> JRadioButton

I discarded the usage of setting the body color. As you can see on the image, you can choose colors and when they are disabled everything grays out.

And here is the HTMLJRadio class, working Ubuntu version:

package so_6648578;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.View;
import sun.swing.SwingUtilities2;

public class HTMLJRadio extends JRadioButton
{

    private static final String HTML_PREFIX = "<html>";

    private static Dimension size = new Dimension();
    private static Rectangle viewRect = new Rectangle();
    private static Rectangle iconRect = new Rectangle();
    private static Rectangle textRect = new Rectangle();
    private String myText;

    public HTMLJRadio(String text)
    {
        super(HTML_PREFIX + text);
        this.myText = text;
    }

    @Override
    public void setEnabled(boolean enabled)
    {
        super.setEnabled(enabled);
        setText(HTML_PREFIX + myText);
    }

    private View clearSuperText()
    {
        try {
            Field textField = getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("text");
            textField.setAccessible(true);
            textField.set(this, null);
            View v = (View) getClientProperty(BasicHTML.propertyKey);

            Field clientPropertiesField = getClass().getSuperclass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("clientProperties");
            clientPropertiesField.setAccessible(true);
            Class arrayTableClass = clientPropertiesField.get(this).getClass();
            Method remove = arrayTableClass.getMethod("remove", Object.class);
            remove.setAccessible(true);
            remove.invoke(clientPropertiesField.get(this), BasicHTML.propertyKey);
            return v;
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
            return null;
        }
    }

    private void restoreSuperText(View v)
    {
        try {
            Field textField = getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("text");
            textField.setAccessible(true);
            textField.set(this, HTML_PREFIX + myText);

            Field clientPropertiesField = getClass().getSuperclass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("clientProperties");
            clientPropertiesField.setAccessible(true);
            Class arrayTableClass = clientPropertiesField.get(this).getClass();
            Method put = arrayTableClass.getMethod("put", Object.class, Object.class);
            put.setAccessible(true);
            put.invoke(clientPropertiesField.get(this), BasicHTML.propertyKey, v);
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }


    @Override
    protected void paintComponent(Graphics g)
    {
        // Paint the icon
        View v = clearSuperText();
        super.paintComponent(g);
        restoreSuperText(v);

        // Paint the HTML
        paintHTML(g);
    }

    public Icon getDefaultIcon()
    {
        return UIManager.getIcon("RadioButton.icon");
    }

    /**
     * paint the radio button
     * StackOverflow.com: Copied and modified from Oracle Java API:
     *
     */
    public synchronized void paintHTML(Graphics g)
    {

        Font f = getFont();
        g.setFont(f);
        FontMetrics fm = SwingUtilities2.getFontMetrics(this, g, f);

        Insets i = getInsets();
        size = getSize(size);
        viewRect.x = i.left;
        viewRect.y = i.top;
        viewRect.width = size.width - (i.right + viewRect.x);
        viewRect.height = size.height - (i.bottom + viewRect.y);
        iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
        textRect.x = textRect.y = textRect.width = textRect.height = 0;

        Icon altIcon = getIcon();

        String text = SwingUtilities.layoutCompoundLabel(
                this, fm, getText(), altIcon != null ? altIcon : getDefaultIcon(),
                getVerticalAlignment(), getHorizontalAlignment(),
                getVerticalTextPosition(), getHorizontalTextPosition(),
                viewRect, iconRect, textRect,
                getText() == null ? 0 : getIconTextGap());

        // fill background
        if (isOpaque()) {
            g.setColor(getBackground());
            g.fillRect(0, 0, size.width, size.height);
        }

        // Draw the Text
        if (text != null) {
            View v = (View) getClientProperty(BasicHTML.propertyKey);
            if (v != null) {
                if (!isEnabled()) {
                    // Perpared the grayed out img
                    BufferedImage img = new BufferedImage(textRect.width + 1, textRect.height + 1, BufferedImage.TYPE_4BYTE_ABGR_PRE);
                    Graphics2D gg = (Graphics2D) img.createGraphics();
                    Rectangle imgRect = new Rectangle(0, 0, textRect.width, textRect.height);
                    gg.setClip(imgRect);
                    v.paint(gg, imgRect);
                    int brighter = getBackground().brighter().getRGB() & 0x00FFFFFF;
                    int darker = getBackground().darker().getRGB() & 0x00FFFFFF;
                    gg.dispose();

                    for (int y = 0; y < img.getHeight(); ++y) {
                        for (int x = 0; x < img.getWidth(); ++x) {
                            int argb = img.getRGB(x, y);
                            if (argb != 0) {
                                argb = (argb & 0xFF000000) | brighter;
                                img.setRGB(x, y, argb);
                            }
                        }
                    }
                    g.drawImage(img, textRect.x + 1, textRect.y + 1, this);


                    for (int y = 0; y < img.getHeight(); ++y) {
                        for (int x = 0; x < img.getWidth(); ++x) {
                            int argb = img.getRGB(x, y);
                            if (argb != 0) {
                                argb = (argb & 0xFF000000) | darker;
                                img.setRGB(x, y, argb);
                            }
                        }
                    }
                    g.drawImage(img, textRect.x, textRect.y, this);

                } else {
                    v.paint(g, textRect);
                }
            } else {
                throw new IllegalStateException("The given text isn't HTML!!");
            }
        }
    }

    public static void showButtons()
    {
        String htmlText = "<h1>Laf</h1><p>Ha Ha!<p style='color: green; text-decoration: underline;'>Green?";
        JPanel p = new JPanel(new GridLayout(0, 1, 3, 3));
        HTMLJRadio button1 = new HTMLJRadio(htmlText);
        p.add(button1);
        HTMLJRadio button2 = new HTMLJRadio(htmlText);
        button2.setEnabled(false);
        p.add(button2);

        JRadioButton button3 = new JRadioButton("Non html disabled");
        button3.setEnabled(false);
        p.add(button3);
        JOptionPane.showMessageDialog(null, p);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run()
            {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                }
                showButtons();
            }
        });
    }
}

Approach 2

Second try to make it work in Windows. This works in Ubuntu as well:

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.View;
import sun.swing.SwingUtilities2;

public class HTMLJRadio extends JRadioButton
{

    private static final String HTML_PREFIX = "<html>";

    private static Dimension size = new Dimension();
    private static Rectangle viewRect = new Rectangle();
    private static Rectangle iconRect = new Rectangle();
    private static Rectangle textRect = new Rectangle();
    private String myText;

    public HTMLJRadio(String text)
    {
        super(HTML_PREFIX + text);
        this.myText = text;
    }

    @Override
    public void setEnabled(boolean enabled)
    {
        super.setEnabled(enabled);
        setText(HTML_PREFIX + myText);
    }

    private View clearSuperText()
    {
        try {
            Field textField = getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("text");
            textField.setAccessible(true);
            textField.set(this, null);
            View v = (View) getClientProperty(BasicHTML.propertyKey);

            Field clientPropertiesField = getClass().getSuperclass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("clientProperties");
            clientPropertiesField.setAccessible(true);
            Class arrayTableClass = clientPropertiesField.get(this).getClass();
            Method remove = arrayTableClass.getMethod("remove", Object.class);
            remove.setAccessible(true);
            remove.invoke(clientPropertiesField.get(this), BasicHTML.propertyKey);
            return v;
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
            return null;
        }
    }

    private void restoreSuperText(View v)
    {
        try {
            Field textField = getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("text");
            textField.setAccessible(true);
            textField.set(this, HTML_PREFIX + myText);

            Field clientPropertiesField = getClass().getSuperclass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("clientProperties");
            clientPropertiesField.setAccessible(true);
            Class arrayTableClass = clientPropertiesField.get(this).getClass();
            Method put = arrayTableClass.getMethod("put", Object.class, Object.class);
            put.setAccessible(true);
            put.invoke(clientPropertiesField.get(this), BasicHTML.propertyKey, v);
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }


    @Override
    protected void paintComponent(Graphics g)
    {
        // Paint the icon
        View v = clearSuperText();
        super.paintComponent(g);
        restoreSuperText(v);

        // Paint the HTML
        paintHTML(g);
    }

    public Icon getDefaultIcon()
    {
        return UIManager.getIcon("RadioButton.icon");
    }

    private Color getDisableColor()
    {
        return UIManager.getColor("ComboBox.disabledForeground");
    }

    private Color getDisableColorBackground()
    {
        return getDisableColor().brighter().brighter();
    }


    /**
     * paint the radio button
     * StackOverflow.com: Copied and modified from Oracle Java API:
     *
     */
    public synchronized void paintHTML(Graphics g)
    {

        Font f = getFont();
        g.setFont(f);
        FontMetrics fm = SwingUtilities2.getFontMetrics(this, g, f);

        Insets i = getInsets();
        size = getSize(size);
        viewRect.x = i.left;
        viewRect.y = i.top;
        viewRect.width = size.width - (i.right + viewRect.x);
        viewRect.height = size.height - (i.bottom + viewRect.y);
        iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
        textRect.x = textRect.y = textRect.width = textRect.height = 0;

        Icon altIcon = getIcon();

        String text = SwingUtilities.layoutCompoundLabel(
                this, fm, getText(), altIcon != null ? altIcon : getDefaultIcon(),
                getVerticalAlignment(), getHorizontalAlignment(),
                getVerticalTextPosition(), getHorizontalTextPosition(),
                viewRect, iconRect, textRect,
                getText() == null ? 0 : getIconTextGap());

        // fill background
        if (isOpaque()) {
            g.setColor(getBackground());
            g.fillRect(0, 0, size.width, size.height);
        }

        // Draw the Text
        if (text != null) {
            View v = (View) getClientProperty(BasicHTML.propertyKey);
            if (v != null) {
                if (!isEnabled()) {
                    // Perpared the grayed out img
                    BufferedImage img = new BufferedImage(textRect.width + 1, textRect.height + 1, BufferedImage.TYPE_4BYTE_ABGR_PRE);
                    Graphics2D gg = (Graphics2D) img.createGraphics();
                    Rectangle imgRect = new Rectangle(0, 0, textRect.width, textRect.height);
                    gg.setClip(imgRect);
                    v.paint(gg, imgRect);

                    Color cBrither = getDisableColorBackground();
                    Color cDarker = getDisableColor();

                    int brighter = cBrither.getRGB() & 0x00FFFFFF;
                    int darker = cDarker.getRGB() & 0x00FFFFFF;

//                    int brighter = getBackground().brighter().getRGB() & 0x00FFFFFF;
//                    int darker = getBackground().darker().getRGB() & 0x00FFFFFF;
                    gg.dispose();

                    for (int y = 0; y < img.getHeight(); ++y) {
                        for (int x = 0; x < img.getWidth(); ++x) {
                            int argb = img.getRGB(x, y);
                            if (argb != 0) {
                                argb = (argb & 0xFF000000) | brighter;
                                img.setRGB(x, y, argb);
                            }
                        }
                    }
                    g.drawImage(img, textRect.x + 1, textRect.y + 1, this);


                    for (int y = 0; y < img.getHeight(); ++y) {
                        for (int x = 0; x < img.getWidth(); ++x) {
                            int argb = img.getRGB(x, y);
                            if (argb != 0) {
                                argb = (argb & 0xFF000000) | darker;
                                img.setRGB(x, y, argb);
                            }
                        }
                    }
                    g.drawImage(img, textRect.x, textRect.y, this);

                } else {
                    v.paint(g, textRect);
                }
            } else {
                throw new IllegalStateException("The given text isn't HTML!!");
            }
        }
    }

    public static void showButtons()
    {
        String htmlText = "<h1>Laf</h1><p>Ha Ha!<p style='color: green; text-decoration: underline;'>Green?";
        JPanel p = new JPanel(new GridLayout(0, 1, 3, 3));
        HTMLJRadio button1 = new HTMLJRadio(htmlText);
        p.add(button1);
        HTMLJRadio button2 = new HTMLJRadio(htmlText);
        button2.setEnabled(false);
        p.add(button2);

        JRadioButton button3 = new JRadioButton("Non html disabled");
        button3.setEnabled(false);
        p.add(button3);
        JOptionPane.showMessageDialog(null, p);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run()
            {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                }
                showButtons();
            }
        });
    }
}

Windows screenshot of 2nd attempt

set default disabled font color for <html> JRadioButton

Approach 3: With determineDisabledColorByWitchCraft():

This one works as well perfect on Ubuntu:

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.View;
import sun.swing.SwingUtilities2;

public class HTMLJRadio extends JRadioButton
{

    private static final String HTML_PREFIX = "<html>";

    private static Dimension size = new Dimension();
    private static Rectangle viewRect = new Rectangle();
    private static Rectangle iconRect = new Rectangle();
    private static Rectangle textRect = new Rectangle();
    private String myText;

    public HTMLJRadio(String text)
    {
        super(HTML_PREFIX + text);
        this.myText = text;
    }

    @Override
    public void setEnabled(boolean enabled)
    {
        super.setEnabled(enabled);
        setText(HTML_PREFIX + myText);
    }

    private View clearSuperText()
    {
        try {
            Field textField = getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("text");
            textField.setAccessible(true);
            textField.set(this, null);
            View v = (View) getClientProperty(BasicHTML.propertyKey);

            Field clientPropertiesField = getClass().getSuperclass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("clientProperties");
            clientPropertiesField.setAccessible(true);
            Class arrayTableClass = clientPropertiesField.get(this).getClass();
            Method remove = arrayTableClass.getMethod("remove", Object.class);
            remove.setAccessible(true);
            remove.invoke(clientPropertiesField.get(this), BasicHTML.propertyKey);
            return v;
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
            return null;
        }
    }

    private void restoreSuperText(View v)
    {
        try {
            Field textField = getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("text");
            textField.setAccessible(true);
            textField.set(this, HTML_PREFIX + myText);

            Field clientPropertiesField = getClass().getSuperclass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("clientProperties");
            clientPropertiesField.setAccessible(true);
            Class arrayTableClass = clientPropertiesField.get(this).getClass();
            Method put = arrayTableClass.getMethod("put", Object.class, Object.class);
            put.setAccessible(true);
            put.invoke(clientPropertiesField.get(this), BasicHTML.propertyKey, v);
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }


    @Override
    protected void paintComponent(Graphics g)
    {
        // Paint the icon
        View v = clearSuperText();
        super.paintComponent(g);
        restoreSuperText(v);

        // Paint the HTML
        paintHTML(g);
    }

    public Icon getDefaultIcon()
    {
        return UIManager.getIcon("RadioButton.icon");
    }


    public static Color determineDisabledColorByWitchCraft() {
        // this is little 'block' character..
        JButton b = new JButton(String.valueOf('\u2586'));
        b.setSize(b.getPreferredSize());
        b.setEnabled(false);
        BufferedImage biDisabled = new BufferedImage(
            b.getWidth(),
            b.getHeight(),
            BufferedImage.TYPE_INT_RGB);
        Graphics g2 = biDisabled.getGraphics();
        b.paint(g2);

        // get the middle pixel..
        int x = b.getWidth()/2;
        int y = b.getHeight()/2;

        return new Color(biDisabled.getRGB(x,y));
    }


    private Color getDisableColor()
    {
        return determineDisabledColorByWitchCraft();
    }

    private Color getDisableColorBackground()
    {
        return getDisableColor().brighter().brighter();
    }


    /**
     * paint the radio button
     * StackOverflow.com: Copied and modified from Oracle Java API:
     *
     */
    public synchronized void paintHTML(Graphics g)
    {

        Font f = getFont();
        g.setFont(f);
        FontMetrics fm = SwingUtilities2.getFontMetrics(this, g, f);

        Insets i = getInsets();
        size = getSize(size);
        viewRect.x = i.left;
        viewRect.y = i.top;
        viewRect.width = size.width - (i.right + viewRect.x);
        viewRect.height = size.height - (i.bottom + viewRect.y);
        iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
        textRect.x = textRect.y = textRect.width = textRect.height = 0;

        Icon altIcon = getIcon();

        String text = SwingUtilities.layoutCompoundLabel(
                this, fm, getText(), altIcon != null ? altIcon : getDefaultIcon(),
                getVerticalAlignment(), getHorizontalAlignment(),
                getVerticalTextPosition(), getHorizontalTextPosition(),
                viewRect, iconRect, textRect,
                getText() == null ? 0 : getIconTextGap());

        // fill background
        if (isOpaque()) {
            g.setColor(getBackground());
            g.fillRect(0, 0, size.width, size.height);
        }

        // Draw the Text
        if (text != null) {
            View v = (View) getClientProperty(BasicHTML.propertyKey);
            if (v != null) {
                if (!isEnabled()) {
                    // Perpared the grayed out img
                    BufferedImage img = new BufferedImage(textRect.width + 1, textRect.height + 1, BufferedImage.TYPE_4BYTE_ABGR_PRE);
                    Graphics2D gg = (Graphics2D) img.createGraphics();
                    Rectangle imgRect = new Rectangle(0, 0, textRect.width, textRect.height);
                    gg.setClip(imgRect);
                    v.paint(gg, imgRect);

                    Color cBrither = getDisableColorBackground();
                    Color cDarker = getDisableColor();

                    int brighter = cBrither.getRGB() & 0x00FFFFFF;
                    int darker = cDarker.getRGB() & 0x00FFFFFF;

//                    int brighter = getBackground().brighter().getRGB() & 0x00FFFFFF;
//                    int darker = getBackground().darker().getRGB() & 0x00FFFFFF;
                    gg.dispose();

                    for (int y = 0; y < img.getHeight(); ++y) {
                        for (int x = 0; x < img.getWidth(); ++x) {
                            int argb = img.getRGB(x, y);
                            if (argb != 0) {
                                argb = (argb & 0xFF000000) | brighter;
                                img.setRGB(x, y, argb);
                            }
                        }
                    }
                    g.drawImage(img, textRect.x + 1, textRect.y + 1, this);


                    for (int y = 0; y < img.getHeight(); ++y) {
                        for (int x = 0; x < img.getWidth(); ++x) {
                            int argb = img.getRGB(x, y);
                            if (argb != 0) {
                                argb = (argb & 0xFF000000) | darker;
                                img.setRGB(x, y, argb);
                            }
                        }
                    }
                    g.drawImage(img, textRect.x, textRect.y, this);

                } else {
                    v.paint(g, textRect);
                }
            } else {
                throw new IllegalStateException("The given text isn't HTML!!");
            }
        }
    }

    public static void showButtons()
    {
        String htmlText = "<h1>Laf</h1><p>Ha Ha!<p style='color: green; text-decoration: underline;'>Green?";
        JPanel p = new JPanel(new GridLayout(0, 1, 3, 3));
        HTMLJRadio button1 = new HTMLJRadio(htmlText);
        p.add(button1);
        HTMLJRadio button2 = new HTMLJRadio(htmlText);
        button2.setEnabled(false);
        p.add(button2);

        JRadioButton button3 = new JRadioButton("Non html disabled");
        button3.setEnabled(false);
        p.add(button3);
        JOptionPane.showMessageDialog(null, p);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run()
            {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                }
                showButtons();
            }
        });
    }
}

Enjoy!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜