Texte in Swing unterstreichen
Um in Swing einen Text auf bestimmte Weise auszuzeichnen, also zum Beispiel halbfett oder kursiv erscheinen zu lassen, reicht ganz wenig Code (stamp
ist ein JLabel
, font
eine Instanz von Font
):
stamp.setFont(font = list.getFont().deriveFont(Font.BOLD));
Oder:
font = list.getFont().deriveFont(Font.PLAIN);
Aber haben Sie mal versucht, Text zu unterstreichen? Die Klasse Font kennt hierfür keine Konstante. Wenn man weiß wie, ist es eigentlich ganz einfach… :-)
Map <TextAttribute, Integer> fontAttributes = new HashMap <TextAttribute,Integer>();
fontAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
font = font.deriveFont(fontAttributes);
This is a (slightly updated) repost of a piece I published on my blog Tommi’s Blog. I deleted the blog in the wake of the GDPR, so the original version is no longer available, or only through the WayBack Machine of the Internet Archive. Please note: code usually has not been updated, so language feature reflect the time the original post was written.