Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Set JLabel text bold

843807Mar 19 2010 — edited Mar 19 2010
Hello Everyone,

here is my problem. I want to set part of the text of a JLabel as bold and another part as underlined, and this is how I am trying:
      	String text = "<html><b>Text one</b> and <u>Text two</html>";
	JLabel jLabel1 = new JLabel(text);
But this is not working. Below it is an example ready to be compiled and runned:
import java.awt.*;
import java.util.*;
import javax.swing.*;

public class Test extends JFrame {

    public Test() {
      	String text = "<html><B>Text one</B> and <u>Text two</html>";
	JLabel jLabel1 = new JLabel(text);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);
        getContentPane().add(jLabel1);
        jLabel1.setBounds(5,5,150,20);	
        pack();
        setSize(200,200);
    }

    public static void main(String args[]) {
         new Test().setVisible(true);            
    }

}
Any help is appreciated
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 16 2010
Added on Mar 19 2010
15 comments
54,636 views