What is the Constant Value of the Underline font in Java?
Asked Answered
I

4

14

What is the Constant Value of the Underline font in Java ?

Font.BOLD bold font

Font.ITALIC italic font

What is the UNDERLINE font Constant ? I try all the available constants but it didn't work .

Illfounded answered 28/11, 2008 at 14:8 Comment(1)
stackoverflow ftw. i was just looking for this today too. i've been using html to get this for a while, but that brings up a whole host of other problems.Argentum
L
20

Suppose you wanted a underlined and bolded Serif style font, size=12.

Map<TextAttribute, Integer> fontAttributes = new HashMap<TextAttribute, Integer>();
fontAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
Font boldUnderline = new Font("Serif",Font.BOLD, 12).deriveFont(fontAttributes);

If you don't want it bolded, use Font.PLAIN instead of Font.BOLD. Don't use the getAttributes() method of the Font class. It will give you a crazy wildcard parameterized type Map<TextAttribute,?>, and you won't be able to invoke the put() method. Sometimes Java can be yucky like that. If you're interested in why, you can check out this site: http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html

Languet answered 13/1, 2009 at 7:30 Comment(3)
The funny thing is that if you are trying to turn underline off you won't find an UNDERLINE_OFF constant. But you can do so by using value -1. EG: fontAttributes.put(TextAttribute.UNDERLINE, -1);Uveitis
+1 for the indication on put not working. I just came across it :) I ended up using AttributeMap att = (AttributeMap)font.getAttributes();, as Font.getAttribute() uses it.Janeljanela
@Languet I implemented your code, for underline but I could not see underline. I am using AWT Label where I want to put the underlined string as you explained.Leaky
C
13

Looking at the Java API Specification, it appears that the Font class does not have a constant for underlining.

However, using the Font(Map<? extends AttributedCharacterIterator.Attribute,?> attributes) constructor, one can give it a Map containing the TextAttribute and the value to use, in order to specify the font attributes. (Note that the TextAttribute class is a subclass of AttributedCharacterIterator.Attribute)

TextAttribute.UNDERLINE seems like the TextAttribute of interest.

Edit: There's an example of using TextAttribute in the Using Text Attributes to Style Text section from The Java Tutorials.

Caro answered 28/11, 2008 at 14:29 Comment(0)
U
3

Underlining is not a property of the font but of the text segment. When rendered the text is rendered in the font specified then a line is drawn under it. Depending on what framework you are using, this may be done for you using properties or you may have to do it yourself.

Unkennel answered 28/11, 2008 at 14:18 Comment(0)
H
0

For SWT you can use:

StyledText text = new StyledText(shell, SWT.BORDER);
text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
// make 0123456789 appear underlined
StyleRange style1 = new StyleRange();
style1.start = 0;
style1.length = 10;
style1.underline = true;
text.setStyleRange(style1);
Hebbel answered 28/11, 2008 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.