How to Underline a JLabel on MouseEnter

11,798

Solution 1

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.

Solution 2

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);
Share:
11,798
user1665700
Author by

user1665700

Updated on June 04, 2022

Comments

  • user1665700
    user1665700 almost 2 years

    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.

  • kleopatra
    kleopatra over 11 years
    hmm ... afaics you forgot to set the derived font back to the label :-)
  • kleopatra
    kleopatra over 11 years
    intended (obviously not strong enough :-) emphasis on derive - we all know that fonts should rarely be created, but derived from the original
  • mKorbel
    mKorbel over 11 years
    @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
  • kleopatra
    kleopatra over 11 years
    as so often, I don't quite understand what you mean ;-) So evolved your answer a bit. +1 for refreshing my memory on attributes
  • mKorbel
    mKorbel over 11 years
    @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
  • secretformula
    secretformula almost 10 years
    Welcome to Stack overflow, can you add an explanation to this answer? Code is alright but explanations are great!