Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set default disabled font color for <html> JRadioButton

Tags:

java

swing

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 JRadioButton {

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:

enter image description here

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).

like image 562
fland Avatar asked Jul 11 '11 10:07

fland


1 Answers

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

Disabled button


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:

CSS in broswer

..renders in JEditorPane like this:

CSS in Swing

:-(

like image 117
Andrew Thompson Avatar answered Oct 09 '22 08:10

Andrew Thompson