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 .

jjnguy
  • 136,852
  • 53
  • 295
  • 323
Waseem
  • 11,741
  • 15
  • 41
  • 45
  • 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. – John Gardner Apr 20 '09 at 21:24

4 Answers4

19

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

Blake
  • 191
  • 2
  • 2
    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);` – Mosty Mostacho Jul 15 '12 at 23:54
  • +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. – Matthieu Jun 03 '13 at 08:20
  • @Blake 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. – Arpit B Parekh May 07 '16 at 10:56
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.

coobird
  • 159,216
  • 35
  • 211
  • 226
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.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
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);
Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66