All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.databasesandlife.util.swing.JCenteredUnderlinedLabel Maven / Gradle / Ivy

package com.databasesandlife.util.swing;

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

/**
 * @author This source is copyright Adrian Smith and licensed under the LGPL 3.
 * @see Project on GitHub
 */
public class JCenteredUnderlinedLabel extends JLabel  {
 public JCenteredUnderlinedLabel(){
  this("");    
  }

 public JCenteredUnderlinedLabel(String text){
  super(text);    
  setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  }

 public void paint(Graphics g) {
  Rectangle r;
  super.paint(g);
  r = g.getClipBounds();
  
  var width = this.getFontMetrics(this.getFont()).stringWidth(this.getText());
  var offset = (getWidth() - width)/2;
  
  g.drawLine
   (offset,
    r.height - this.getFontMetrics(this.getFont()).getDescent() +1, 
    offset + width,  
    r.height - this.getFontMetrics(this.getFont()).getDescent() +1);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy