The Java Man
The Java Man

Reputation: 305

How do I put html in a JLabel in java?

How do I use html tags in a JLabel in java?

Upvotes: 19

Views: 36734

Answers (6)

user2396528
user2396528

Reputation: 11

JLabel myHTMLLabel =new JLabel("<html>");
myHTMLLabel.setText("<html><font color='green'>Hello World</font>");

Upvotes: 1

Malith
Malith

Reputation: 401

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>");

Upvotes: 2

james_bond
james_bond

Reputation: 6906

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

Upvotes: 9

whirish
whirish

Reputation: 470

This should do the trick:

JLabel whatever =
    new JLabel("<html><something>Put Stuff Here</something></html>");

Upvotes: 1

Vivek
Vivek

Reputation: 13278

There are following ways

  1. Using SetText method of JLabel Object

    JLabel HTMLlabel = new JLabel().setText("<html><tag>blah blah</tag></html>");

  2. Passing String to JLable class Constructor.

    JLabel HTMLlabel = new JLabel("<html><tag>blah blah</tag></html>");

  3. 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);

Upvotes: 5

Globmont
Globmont

Reputation: 901

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>");

Upvotes: 21

Related Questions