PDA

View Full Version : HTML Tags in JTextPane/JFrame



pclip123
03-23-2007, 10: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?

joakimk
05-29-2007, 07: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>");

alexjewell
06-05-2007, 01:43 AM
or use:



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


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

Twey
06-05-2007, 03: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.

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

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

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