We help IT Professionals succeed at work.

html code inside jlabel prevents display of text

sdholden28
sdholden28 asked
on
406 Views
Last Modified: 2013-11-23
I have a jlabel in a program I am working on that I needs to display text on multiple lines. Supposedly a jlabel supports simple HTML so the following should work:

JLabel label = new JLabel("<html>New line.<br> testing</html>");      
      firstFrame.add(label);

However when the html tags are present, my jframe opens, but the text is not displayed. As soon as i remove the tags, the text is displayed, but of course not formatted correctly. It doesn't seem to matter what particular html i use inside the tags, nothing displays.
What's going on?


Comment
Watch Question

Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
<html> marks the beginning and end of a web page.  Is that what you want?  Maybe try <p> and </p>.

Author

Commented:
I'm just trying to format the text in the label correctly by forcing a new line when i want to. "<html>some text<br> now new line </html>" should work according to all sources I can find as the jlabel component supports html formatting. However in my case, for whatever reason, any html tags cause text not to display at all.
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
No, it shouldn't work.  The <html> tag itself doesn't cause any formatting.  Below is the format for creating a web page.  Only appropriate tags in the <body> section will cause display formatting.  If you are trying to display your text in an already existing page, you will just confuse the browser by using the <html> tag the way you showed above.
<html>
<head>
<title>Untitled</title>
</head>
<body>
<p>Text goes here!</p>
</body>
</html>

Open in new window

Author

Commented:
Dave you're not understanding what I'm trying to do. I'm not doing anything with a web page at all. I'm writing a program in java, and I'm trying to use html to format text output in a jlabel component of a jframe.

See this link:
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/components/html.html

For whatever reason, this doesn't work for me. That's what I'm trying to figure out.
Fixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Got it. Apparently when using html formatting, the setvisible property must be changed to true after the jlabel is added. If there is no html tags then it doesn't seem to matter, so I didn't catch it, but with html tags, text will only displayed if jlabel is already added when setvisible is modified. Thanks for bouncing with me Dave, sometimes thats all you need.
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
You're welcome.  Thanks for the points.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Apparently when using html formatting, the setvisible property must be changed to true after the jlabel is added.

thats not the case

Author

Commented:
Then please elaborate. In my current code, of I setvisible to true before I add the jlabel it doesn't show. If I set visible to true after I add the jlabel, it formats and displays correctly. That was the basis for my comment. Please correct me if I'm wrong.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
ned to see your current code
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.