View Full Version : HTML Tags in JTextPane/JFrame

03-23-2007, 09:48 PM
I have the following method declared..

public void display() {
Set finalStringSet = mapFinal.keySet();
JTextPane textPane = new JTextPane(); // creates an empty text pane
textPane.setContentType("text/html"); // lets Java know it will be HTML
textPane.setText(<span style="font-size: 20pt">Big</span>); // sets its text
JFrame frame = new JFrame(); // makes a window to put it in
frame.getContentPane().add(textPane); // adds the text pane to the window
frame.pack(); // adjusts the window to the right size
frame.setVisible(true); // makes it show up

I am having trouble with the line that reads
textPane.setText(<span style="font-size: 20pt">Big</span>);

I just want to print out the string "big" with that increased font size, yet everytime i try to compile i get the error "> expected".. Can someone PLEASE help me?

05-29-2007, 06:25 AM
Have you tried adding quotation marks around the string you're printing? Remeber to escape the quotes you're using as part of your string;

textPane.setText("<span style=\"font-size: 20pt\">Big</span>");

06-05-2007, 12:43 AM
or use:

textPane.setText('<span style="font-size: 20pt;">Big</span>');

The slashes get on my nerves. Besides, single quotes parse quicker.

06-05-2007, 02:43 PM
alexjewell, this is Java, not PHP. Single quotes are used to denote character literals, not strings.

joakimk's method is the correct one.

06-06-2007, 04:56 PM
Ha, oops. I apologize.

10-20-2010, 09:29 PM
This also works if escaping is not desired.

textPane.setText("<span style='font-size: 20pt'>Big</span>");