Skip to content

Instantly share code, notes, and snippets.

@rodrigocananea
Last active February 9, 2023 17:22
Show Gist options
  • Save rodrigocananea/f337013c4d09f42a68bcddaa375c3517 to your computer and use it in GitHub Desktop.
Save rodrigocananea/f337013c4d09f42a68bcddaa375c3517 to your computer and use it in GitHub Desktop.
Java Swing Component JLabel with option 'requiredShow' https://prnt.sc/cD4v5CngTiWf
import com.formdev.flatlaf.ui.FlatTextBorder;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
/**
*
* @author Rodrigo Cananea <rodrigoaguiar35@gmail.com>
*/
public class ELabel extends JLabel {
public ELabel() {
super();
super.setBorder(BorderFactory.createCompoundBorder(new FlatTextBorder(), BorderFactory.createEmptyBorder(0, 8, 0, 8)));
}
public boolean isBorderLineShow() {
return borderLineShow;
}
public void setBorderLineShow(boolean borderLineShow) {
this.borderLineShow = borderLineShow;
}
public boolean isRequiredShow() {
return requiredShow;
}
public void setRequiredShow(boolean requiredShow) {
this.requiredShow = requiredShow;
}
private boolean borderLineShow;
private boolean requiredShow;
@Override
protected void paintBorder(Graphics grphcs) {
if (borderLineShow) {
Graphics2D g2 = (Graphics2D) grphcs.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color line = Color.BLACK;
g2.setColor(line);
int height = getHeight();
g2.drawRoundRect(0, 0, getWidth() - 1, height - 1, height - 3, height - 3);
}
super.paintBorder(grphcs);
}
@Override
public void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
if (requiredShow) {
createRequiredMark(grphcs);
}
}
private void createRequiredMark(Graphics grphcs) {
String requiredText = "*";
int requiredMargin = 4;
Insets insets = new Insets(0, requiredMargin, 0, requiredMargin);
Border border = getBorder();
if (border instanceof EmptyBorder) {
EmptyBorder emptyBorder = (EmptyBorder) border;
Insets insetsBorder = emptyBorder.getBorderInsets();
insets.left += insetsBorder.left;
insets.right += insetsBorder.right;
}
Graphics2D g2d = (Graphics2D) grphcs;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g2d.setColor(Color.red);
Font font = getFont();
g2d.setFont(new Font(font.getFamily(), Font.PLAIN, font.getSize()));
FontMetrics fm = g2d.getFontMetrics();
int x, y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
int align = getHorizontalAlignment();
String text = getText();
switch (align) {
case LEADING:
case LEFT:
x = fm.stringWidth(text) + insets.left;
break;
case RIGHT:
x = (getWidth() - fm.stringWidth(text)) - insets.left - 2;
break;
default: // Centralizar
x = ((getWidth() - fm.stringWidth(text)) / 2) + fm.stringWidth(text) + insets.right;
break;
}
g2d.drawString(requiredText, x, y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment