1
JLabel topCap = new JLabel ("  Top Caption");
txtTop = new JTextField("Enter top Caption here");
topCaption = new JLabel("", JLabel.CENTER);
viewerWindow.add(topCaption, BorderLayout.NORTH);
JLabel bottomCap = new JLabel ("  Bottom Caption");
txtBottom = new JTextField("Enter Bottom Caption here");
bottomCaption = new JLabel("",JLabel.CENTER);
viewerWindow.add(bottomCaption,BorderLayout.SOUTH);

I have another block of code in which when the user enters text into the JTextField, and presses the update JButton it is displayed to the JFrame. This works fine for all styles (italic and bold) except underline. I've looked everywhere and I found a block of code on stackoverflow but that didn't help either. In case you're wondering, this is the line of code that didn't work:

JLabel label = new JLabel("Underlined Label");
Font font = label.getFont();
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
label.setFont(font.deriveFont(attributes));

I am thoroughly confused does anyone know what i should do? Or at least what class i should look into? thanks

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
a. zaidi
  • 31
  • 1
  • 5
  • Post a [mcve] to explain what's not working – Reimeus Apr 10 '16 at 02:09
  • @Reimeus I'm not exactly sure at all what's not working. when i use the block of code that i put at the bottom, i get an error message saying "cannot find class Map", but i went to the Java Library and could not find any class Map there either. – a. zaidi Apr 10 '16 at 02:43

2 Answers2

1

You must import TextAttribute from awt

java.awt.font.TextAttribute;

You can use HTML to underline. But for faster rendering than HTML, you should stick to your method.

JLabel.setText("<HTML><U>Underlined Text</U></HTML>");
  • I have already imported that, and still no luck. – a. zaidi Apr 10 '16 at 02:29
  • can you post the exact error message or explain what's not being executed? – Tahmid Munat Apr 10 '16 at 02:36
  • there is no error message, just the JLabel that is on screen does not change at all. I have a JRadioButton that when pressed, should change the text in the JLabel to be underlined. I have the ActionListener attached correctly because i have tested it with other font styles (bold and italic). – a. zaidi Apr 10 '16 at 02:39
0

You can easily use HTML inside JLabel. As a example you can do like this.

JLabel label=new JLabel();
label.setText("<html><u>Underlined Label</u></html>");
Lasitha Yapa
  • 4,309
  • 8
  • 38
  • 57
  • and if i want to change the size as well as underline would i invoke the setFont method then? – a. zaidi Apr 10 '16 at 02:26
  • In that case I think you can take a tip from this [link](http://stackoverflow.com/questions/2715118/how-to-change-the-size-of-the-font-of-a-jlabel-to-take-the-maximum-size) – Lasitha Yapa Apr 12 '16 at 07:01
  • It is not working in my case. I am using WindowsClassicLookAndFeel. With default L&F it works. So it is a bug. – WesternGun Sep 20 '17 at 09:24