Java: Java and HTML

Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

DYS

Thread Starter
Joined
Dec 28, 2008
Messages
462
Hi people.

I simply want to have a JLabel print out a String on multiple lines. I was quite surprised to find that this isn't a basic and initial option.
Anyway, I read on Oracle.com that one can use HTML in Java, which would apparently allow me to insert simple <br> tags for a line break.
However, I carried out a simple test and followed what that tutorial said. This is what I did:

Code:
public class Test {
	public static void main (String[] args)
	{
		String s = "<html>Hello World</html>";
		System.out.println(s);
	}  
}
As I suspected, the printed output reads: "<html>Hello World</html>".

I gathered this would happen, since the HTML tags are within the inverted commas; however, that's what the tutorial says to do.

I haven't imported any other classes, as as far as I can see from the tutorial (unless I've completely missed it), it doesn't say that I need to.

Can anyone point me in the right direction, please?

Thanks.
 
Joined
Jul 2, 2006
Messages
379
I believe that you can only use html in swing components, so that's things like JLabels, JButtons etc
Code:
JLabel label = new JLabel("<html>Hello<br/>World!</html>");
 
Joined
Sep 25, 2007
Messages
290
From the tutorial:
If you want to mix fonts or colors within the text, or if you want formatting such as multiple lines, you can use HTML. HTML formatting can be used in all Swing buttons, menu items, labels, tool tips, and tabbed panes, as well as in components such as trees and tables that use labels to render text.
It doesn't say you can use HTML with System.out.println(), because you can't.
 

DYS

Thread Starter
Joined
Dec 28, 2008
Messages
462
Right... Okay thanks. I'll give this a go and get back to you.

Cheers (both).
 
Joined
Oct 30, 2008
Messages
276
I may have this wrong, but I think you would actually like to add a break to the string and not necessarily use HTML. In java, the char '/' works as a delimiting character. When working with strings, you can create a line break by following the delimiting character with an n, so it looks like /n. If I wanted Hello on one line, and World on the next,
Code:
 s = "Hello /nWorld"
Interestingly, when you use strings to hold paths, this will come up. "http://www.techguy.org" becomes "http:////www.techguy.org"

gl
 
Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Top