简体   繁体   中英

How to Underline a JLabel on MouseEnter

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.

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 fi JXHyperlink in the SwingX project . You might want to run the demo referenced on its project home.

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

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);
    }
 if (CheckBox.isSelected()) {
        Font font = CheckBox.getFont();
        Map attributes = font.getAttributes();
        attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_GRAY);
        CheckBox.setFont(font.deriveFont(attributes));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM