Have a Label with underlined text : Text « 2D Graphics GUI « Java






Have a Label with underlined text

   

import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JLabel;

class UnderlinedLabel extends JLabel {
  public UnderlinedLabel() {
    this("");
  }

  public UnderlinedLabel(String text) {
    super(text);
  }

  public void paint(Graphics g) {
    Rectangle r;
    super.paint(g);
    r = g.getClipBounds();
    g.drawLine(0, r.height - getFontMetrics(getFont()).getDescent(), getFontMetrics(getFont())
        .stringWidth(getText()), r.height - getFontMetrics(getFont()).getDescent());
  }
}

   
    
    
  








Related examples in the same category

1.Create a shadowed text
2.Draw 2D Text
3.Drawing Simple Text
4.Drawing Rotated Text
5.Draw string rotated clockwise 45 degrees
6.Draw string rotated counter-clockwise 45 degrees
7.Getting the Dimensions of Text
8.Display underlined text
9.Display vertical text
10.Use AffineTransform to draw vertical text
11.Display some lyrics on the panel.
12.Display unicode text
13.drawString(): specify the position of the text on the window areadrawString(): specify the position of the text on the window area
14.Rotate a line of character (String)
15.Generate Shape From Text