Question

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.

Was it helpful?

Solution 3

I ended up solving the problem of not being able to underline text by surrounding my string with ..... tags.

OTHER TIPS

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

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);  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top