0

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.

Charles Sprayberry
  • 7,741
  • 3
  • 41
  • 50
Mike2012
  • 7,629
  • 15
  • 84
  • 135

3 Answers3

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
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