Creating an underlined JLabel without using HTML

Main.java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class Main extends JFrame
{
   public Main() {
      getContentPane().setLayout(new GridLayout(1, 2));
      JPanel panel1 = new JPanel();
      panel1.setLayout(new GridLayout(5, 1));
      panel1.add(new JLabel("JLabel/icon/LEFT", 
                           new ImageIcon("tiffIcon.gif"), SwingConstants.LEFT));
      panel1.add(new JLabel("JLabel/icon/CENTER", 
                           new ImageIcon("tiffIcon.gif"), SwingConstants.CENTER));
      panel1.add(new JLabel("JLabel/icon/RIGHT", 
                           new ImageIcon("tiffIcon.gif"), SwingConstants.RIGHT));
      panel1.add(new JLabel("JLabel/icon/LEADING", 
                           new ImageIcon("tiffIcon.gif"), SwingConstants.LEADING));
      panel1.add(new JLabel("JLabel/icon/TRAILING", 
                           new ImageIcon("tiffIcon.gif"), SwingConstants.TRAILING));
 
      getContentPane().add(new JScrollPane(panel1));
 
      JPanel panel2 = new JPanel();
      panel2.setLayout(new GridLayout(5, 1));
      panel2.add(new UnderlinedJLabel("JLabel/icon/LEFT", 
                                      new ImageIcon("tiffIcon.gif"), SwingConstants.LEFT));
      panel2.add(new UnderlinedJLabel("JLabel/icon/CENTER", 
                                      new ImageIcon("tiffIcon.gif"), SwingConstants.CENTER));
      panel2.add(new UnderlinedJLabel("JLabel/icon/RIGHT", 
                                      new ImageIcon("tiffIcon.gif"), SwingConstants.RIGHT));
      panel2.add(new UnderlinedJLabel("JLabel/icon/LEADING", 
                                      new ImageIcon("tiffIcon.gif"), SwingConstants.LEADING));
      panel2.add(new UnderlinedJLabel("JLabel/icon/TRAILING", 
                                      new ImageIcon("tiffIcon.gif"), SwingConstants.TRAILING));
 
      getContentPane().add(new JScrollPane(panel2));
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 150);
      main.setVisible(true);   
   }
}
 
class UnderlinedJLabel extends JLabel
{
   public UnderlinedJLabel() {
   }
 
   public UnderlinedJLabel(Icon image) {
      super(image);
   }
 
   public UnderlinedJLabel(Icon image, int horizontalAlignment) {
      super(image, horizontalAlignment);
   }
 
   public UnderlinedJLabel(String text) {
      super(text);
   }

   public UnderlinedJLabel(String text, Icon icon, int horizontalAlignment) {
      super(text, icon, horizontalAlignment);
   }
 
   public UnderlinedJLabel(String text, int horizontalAlignment) {
      super(text, horizontalAlignment);
   }
 
   public void paint(Graphics g) {
      super.paint(g);
      underline(g);
   }
 
   protected void underline(Graphics g) {
      Insets insets = getInsets();
      FontMetrics fm = g.getFontMetrics();
      Rectangle textR = new Rectangle();
      Rectangle viewR = new Rectangle(
                                  insets.left, 
                                  insets.top, 
                                  getWidth() - (insets.right + insets.left), 
                                  getHeight() - (insets.bottom + insets.top));
  
      // compute and return location of the icons origin,
      // the location of the text baseline, and a possibly clipped
      // version of the compound label string.  Locations are computed 
      // relative to the viewR rectangle.
      String text = SwingUtilities.layoutCompoundLabel(
                         this,                        // this JLabel
                         fm,                          // current FontMetrics
                         getText(),                   // text
                         getIcon(),                   // icon
                         getVerticalAlignment(),      
                         getHorizontalAlignment(),
                         getVerticalTextPosition(),
                         getHorizontalTextPosition(), 
                         viewR,                       
                         new Rectangle(),             // don't care about icon rectangle
                         textR,                       // resulting text locations
                         getText() == null ? 0 : 
                            ((Integer)UIManager.get("Button.textIconGap")).intValue());
 
      // draw line
      int textShiftOffset = ((Integer) UIManager.get("Button.textShiftOffset")).intValue();
      g.fillRect(textR.x +
                 textShiftOffset - 4,
                 textR.y + fm.getAscent() + textShiftOffset + 2,
                 textR.width, 
                 1);
   }
}