Results 1 to 6 of 6

Thread: HTML Tags in JTextPane/JFrame

  1. #1
    Join Date
    Mar 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HTML Tags in JTextPane/JFrame

    I have the following method declared..

    Code:
    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
    Code:
    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?

  2. #2
    Join Date
    May 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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>");
    Last edited by joakimk; 05-29-2007 at 07:16 AM.

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    or use:

    Code:
    textPane.setText('<span style="font-size: 20pt;">Big</span>');
    The slashes get on my nerves. Besides, single quotes parse quicker.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

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

    joakimk's method is the correct one.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Ha, oops. I apologize.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  6. #6
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    This also works if escaping is not desired.

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •