Underline a stringTag(s): Swing


Since Swing (JDK1.2) implements simple HTML rendering for its components, it's possible to display underlined string (on JLabel or JButton for example).

HTML tags must be in lowercase and simple.

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

public class UnderlineInSwing extends JFrame {
 public UnderlineInSwing() {

        super("Underline In Swing");
        setSize(400, 300);

        getContentPane().setLayout(new FlowLayout());

        String htmlText = 
            "<html><p><font color=\"#800080\" "+
            "size=\"4\" face=\"Verdana\">HTML in JLabel</font></p>"+
            "<font size=\"2\"><u>"+
            "underline is possible</u><br><b> and bold too</b></font>"+
            "";
        JLabel lbl = new JLabel(htmlText);
        getContentPane().add(lbl);

        WindowListener wndCloser = new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(0);
                }
            };
        addWindowListener(wndCloser);
        setVisible(true);
    }

    public static void main(String args[]){
     new UnderlineInSwing();
     }
}

blog comments powered by Disqus