0

when i use this code it does not work; the text does not become bold. Why?

    label1 = new JLabel();
    label1.setText("Welcome, <html><strong>Hussein</strong></html>.");
Boolena
  • 225
  • 1
  • 2
  • 10
  • Did you check http://stackoverflow.com/questions/1790500/render-html-in-swing-application – Hirak Apr 11 '14 at 12:46

4 Answers4

4

Your HTML syntax is bad, since your String does not start with the html DOM root.

Try something in the lines of:

label1.setText("<html>Welcome <strong>Hussein</strong>.</html>");

Find a tutorial here.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • Maybe also add a body tag? – Déjà vu Apr 11 '14 at 12:45
  • @DejaVu nope. Not necessary. – Mena Apr 11 '14 at 12:46
  • 1
    @DejaVu "To specify that a component's text has HTML formatting, just put the tag at the beginning of the text, then use any valid HTML in the remainder." http://docs.oracle.com/javase/tutorial/uiswing/components/html.html – Erwin Bolwidt Apr 11 '14 at 12:48
  • 1
    @DejaVu this is 1) not a script 2) conforms to API, which implies that 3) html in Swing components does not have to fully conform to w3c standards. – Mena Apr 11 '14 at 12:51
0

You forgot to add body tag ,Try This :

label1 = new JLabel();
    label1.setText("<html><body>Welcome ,<strong> Hussein</strong></body></html>");
Raju Sharma
  • 2,496
  • 3
  • 23
  • 41
  • i have edited, it wont accept , if we enter text with mixed type string+html tag. it will be working fine as you want here. – Raju Sharma Apr 11 '14 at 12:48
0

try this:

JLabel label = new JLabel("<html><yourTagHere><yourOtherTagHere>this is your text</yourOtherTagHere></yourTagHere></html>");
ZaoTaoBao
  • 2,567
  • 2
  • 20
  • 28
-1

Why are you mixing html and java. tag is in html. This is how you do in java

label = new JLabel("A label");
label.setFont(new Font("Serif", Font.BOLD, 14));
Stunner
  • 961
  • 2
  • 19
  • 39
  • 1
    i only wanted a part to be in bold, not all of it. I am aware of this and i do use it. – Boolena Apr 11 '14 at 12:46
  • Then , you should mention in your question , saying clearly that you want it only for **some part of text** – Stunner Apr 11 '14 at 12:50
  • @Stunner Your answer is saying that you can't use html in a JLabel component (or not even in Java), which is not true. – Erwin Bolwidt Apr 11 '14 at 13:09