How do I use html tags in a JLabel in java?
Asked
Active
Viewed 3.4k times
18
-
Why would you want to do that? – duffymo Jul 09 '11 at 15:42
-
2`JLabel label = new JLabel().setText("
blah blah ");` – Eng.Fouad Jul 09 '11 at 15:43 -
2This strikes me as a bad idea. – Jeremy Jul 09 '11 at 15:44
-
A very bad idea, indeed. – duffymo Jul 09 '11 at 15:54
-
18Why is this a bad idea? Its even documented in the Swing tutorial: http://download.oracle.com/javase/tutorial/uiswing/components/html.html – camickr Jul 09 '11 at 15:54
-
11it is a quite effective way of doing simple formatting in your text areas... – Randy Jul 09 '11 at 16:02
6 Answers
20
To put html in a JLabel
, you would make it look something like this
JLabel label = new JLabel("<html><yourTagHere><yourOtherTagHere>this is your text</yourOtherTagHere></yourTagHere></html>");

Globmont
- 881
- 2
- 8
- 20
7
This will do the trick:
String labelText ="<html><FONT COLOR=RED>Red</FONT> and <FONT COLOR=BLUE>Blue</FONT> Text</html>";
JLabel coloredLabel =new JLabel(labelText);

james_bond
- 6,778
- 3
- 28
- 34
5
There are following ways
Using SetText method of JLabel Object
JLabel HTMLlabel = new JLabel().setText("<html><tag>blah blah</tag></html>");
Passing String to JLable class Constructor.
JLabel HTMLlabel = new JLabel("<html><tag>blah blah</tag></html>");
Using String and passing it to JLabel class Constructor similar to above example but using String.
String HTMLlabelStr = "<html><tag>blah blah</tag></html>";
JLabel HTMLlabel = new JLabel(HTMLlabelStr);

om-nom-nom
- 62,329
- 13
- 183
- 228

Vivek
- 11,938
- 19
- 92
- 127
1
JLabel myHTMLLabel =new JLabel("<html>");
myHTMLLabel.setText("<html><font color='green'>Hello World</font>");

Pang
- 9,564
- 146
- 81
- 122

user2396528
- 11
- 1
1
Also you can use this with all Swing buttons, menu items, labels, text panes, editor panes, tool tips, tabbed panes etc...
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setText("<html><h1>My First Heading</h1><p>My first paragraph.</p></body></html>");

Malith
- 381
- 6
- 18