[ACCEPTED]-What is the Constant Value of the Underline font in Java?-underline

Accepted answer
Score: 18

Suppose you wanted a underlined and bolded 8 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 7 it bolded, use Font.PLAIN instead of Font.BOLD. Don't 6 use the getAttributes() method of the Font 5 class. It will give you a crazy wildcard 4 parameterized type Map<TextAttribute,?>, and you won't be able 3 to invoke the put() method. Sometimes Java 2 can be yucky like that. If you're interested 1 in why, you can check out this site: http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html

Score: 13

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

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

TextAttribute.UNDERLINE seems like the TextAttribute of interest.

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

Score: 3

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

Score: 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;


More Related questions