3

I tried to change the font, by using:

jLabel.setFont(new Font("Tahoma",1,20));

But there's only 4 styles here, Plain, Bold, Italic, Bold+Italic.

I want it to work like a link in HTML, the JLabel gets underlined when I hover the mouse cursor on it.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1665700
  • 512
  • 2
  • 13
  • 28

4 Answers4

6

To clarify (or not :-) the confusion introduced in my comments to mKorbel

Never create a Font out of the blue: it will most probably clash with all other fonts in the application. Instead, grab the default (either from the component instance as in the snippet shown below or the UIManager, doesn't matter) and derive.

For deriving using attributes (shamelessly steeling from mKorbel's answer), that's something like

JLabel label = new JLabel("some text - WE ARE UNDERLINED");
MouseListener l = new MouseAdapter() {
    Font original;

    @Override
    public void mouseEntered(MouseEvent e) {
        original = e.getComponent().getFont();
        Map attributes = original.getAttributes();
        attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
        e.getComponent().setFont(original.deriveFont(attributes));
    }

    @Override
    public void mouseExited(MouseEvent e) {
        e.getComponent().setFont(original);
    }


};
label.addMouseListener(l);
JComponent content = new JPanel();
content.add(label);
content.add(new JButton("dummy focus"));

But beware: that will not yet give you any hyperlink functionality! So in case a Hyperlink is what you are really after, consider using a full-fledged component with such a functionality, like f.i. JXHyperlink in the SwingX project. You might want to run the demo referenced on its project home.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
4

use for proper MouseEvent

JLabel#setFont(new Font(attributes));

and back

JLabel#setFont(new Font("Serif", Font.BOLD, 16));

wrapped into invokeLater, and from definitions

final Map attributes = (new Font("Serif", Font.BOLD, 16)).getAttributes();
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • hmm ... afaics you forgot to set the derived font back to the label :-) – kleopatra Sep 12 '12 at 13:39
  • intended (obviously not strong enough :-) emphasis on _derive_ - we all know that fonts should rarely be created, but derived from the original – kleopatra Sep 12 '12 at 13:49
  • @kleopatra this Font isn't declared into UIManager, no longer after first time is used for JComponents, isn't it, [please no idea what did you talking about](http://stackoverflow.com/a/8582656/714968) – mKorbel Sep 12 '12 at 13:59
  • as so often, I don't quite understand what you mean ;-) So evolved your answer a bit. +1 for refreshing my memory on attributes – kleopatra Sep 12 '12 at 15:13
  • @kleopatra sorry, but I can't found of remember (sure excluding Nimbus and Substance) that somewhere is/are issue(s) with Font a Color for Standard JComponents in Java6 (excluding a few Bugs from Java 2-3), or her majesty knows something else, can you please to share yours `with drum to the hare` – mKorbel Sep 13 '12 at 04:51
0

Use this with required CSS,

yourLabel.setText(htmlIfy("<p style='color:#1C66AE;'>Your text here</p>"));

where the htmlIfy function is

private static final String HTML = "<html>";
    private static final String HTML_END = "</html>";


public static String htmlIfy(String s) {
        return HTML.concat(s).concat(HTML_END);
    }

to add text like link use

yourLabel.setText(HTMLTagUtil.htmlIfy(HTMLTagUtil
                .linkIfy("Your Text Here")));//Forgot Password?

        yourLabel.setCursor(new java.awt.Cursor(
                java.awt.Cursor.HAND_CURSOR));

where the linkIfy function is

private static final String A_HREF = "<a href=\"";
    private static final String HREF_CLOSED = "\">";
    private static final String HREF_END = "</a>";
public static String linkIfy(String s) {
        return A_HREF.concat(s).concat(HREF_CLOSED).concat(s).concat(HREF_END);
    }
NoNaMe
  • 6,020
  • 30
  • 82
  • 110
0
 if (CheckBox.isSelected()) {
        Font font = CheckBox.getFont();
        Map attributes = font.getAttributes();
        attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_GRAY);
        CheckBox.setFont(font.deriveFont(attributes));
    }
Said Erraoudy
  • 1,490
  • 1
  • 13
  • 21