tags:

views:

61

answers:

3

when i search internet i have found a way to underline font like this,

Font f=jLabel1.getFont();
Map<TextAttribute,Object> map = new Hashtable<TextAttribute,Object>();
map.put(TextAttribute.UNDERLINE,TextAttribute.UNDERLINE_ON);
f=f.deriveFont(map);
jLabel1.setFont(f);

it works well on jdk6, however it doesnt work on jdk5, and it doesnt warn about anything. first, how can i get same effect on jdk5? second, why is there a TextAttribute.UNDERLINE constant, if it doesnt work?

+2  A: 

I'm not aware of any way except the somewhat ugly HTML-approach:

label.setText("<html>some <u>underlined</u> text</html>");

Beware however, that if you provide a custom look-and-feel, the HTML-rendering may no longer work as expected.

Another way would be to add a MatteBorder to the label, but that "underlines" the entire label:

label.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
aioobe
A: 

I think this code can work in JDK.5 text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ"); // make 0123456789 appear underlined obj.underline=true may work fine.

  StyleRange style1 = new StyleRange();
    style1.start = 0;
    style1.length = 10;
    style1.underline = true;
    text.setStyleRange(style1);

0123456789 is underlined.
giri
What do you mean? There is no setStyleRange for a JLabel.
aioobe
i dont know much about SWT, but google makes me feel that StyleRange is something SWT specific.
Serkan Kasapbaşı
A: 

Many bugs have been reported against the deriveFont methods; see this search. I could not spot one that exactly matched your problem, but you could have better luck. If you find a relevant bug report, there may be a workaround.

Stephen C