Helpful Information
 
 
Category: Java
HTML Tags in JTextPane/JFrame

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?

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>");

or use:



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


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

alexjewell, this is Java, not PHP. Single quotes are used to denote character literals, not strings.

joakimk's method is the correct one.

Ha, oops. I apologize.

This also works if escaping is not desired.

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










privacy (GDPR)