I need to make a font underline and blue for a hyperlink in one of my JButtons, but it seems the font class has no obvious way to do this. I can't use attributedtext because I'm not going to be displaying this with Graphics class. Is there anyway I can accomplish this? I just need the title of my JButton to be blue and underlined.
Asked
Active
Viewed 2,598 times
3 Answers
3
I am too late to reply. But anyways, I am going to post it here. Maybe it would be helpful to someone.
JButton button = new JButton("Label");
HashMap<TextAttribute, Object> textAttrMap = new HashMap<TextAttribute, Object>();
textAttrMap.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
textAttrMap.put(TextAttribute.FOREGROUND, Color.BLUE);
button.setFont(button.getFont().deriveFont(textAttrMap));
Ref: http://docs.oracle.com/javase/tutorial/2d/text/examples/AttributedText.java

user1401250
- 336
- 2
- 7
0
JButton button = new JButton("OK");
button.setBackground(Color.blue);
Font buttonFont=new Font(button.getFont().getName(),Font.UNDERLINED+Font.BOLD,button.getFont().getSize());
button.setFont(buttonFont);

RJFalconer
- 10,890
- 5
- 51
- 66
-
I think the font underline constant may actually be "Font.UNDERLINE". Try both. – RJFalconer Aug 07 '09 at 00:43
-
Ah, see: http://stackoverflow.com/questions/325840/what-is-the-constant-value-of-the-underline-font-in-java – RJFalconer Aug 07 '09 at 00:44
0
I ended up solving the problem of not being able to underline text by surrounding my string with ..... tags.

Mike2012
- 7,629
- 15
- 84
- 135