Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Make JTextArea Support HTML

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Make JTextArea Support HTML

    I need to know how to make JTextArea or JEditorPane able to support HTML coding.

    Could anyone give me an example of how to do this?

    Any help would be appreciated! Thank you in advance.

    P.S. Please do not link me to the Java Tutorial page or API section for either component. I have failed to understand them on this subject in the past, so I don't believe I will suddenly find them useful now.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,104
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Make JTextArea Support HTML

    What have you found when you search for JEditorPane code examples?

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Make JTextArea Support HTML

    Quote Originally Posted by bgroenks96 View Post
    P.S. Please do not link me to the Java Tutorial page or API section for either component. I have failed to understand them on this subject in the past, so I don't believe I will suddenly find them useful now.
    Anything we post would just be a rehash (and a poor one at that) of the information already provided in those 2 resources. So what makes you think that reading our post(s) will suddenly make you understand?
    Improving the world one idiot at a time!

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Make JTextArea Support HTML

    Quote Originally Posted by Junky View Post
    Anything we post would just be a rehash (and a poor one at that) of the information already provided in those 2 resources. So what makes you think that reading our post(s) will suddenly make you understand?
    A different scenario often helps with the comprehension process.

    And the JTextPane tutorial provides very little code examples.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,104
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Make JTextArea Support HTML

    After looking at the API doc for the JEditorPane for a bit I tried this:
            JEditorPane jep = new JEditorPane();
            jep.setContentType("text/html");
            jep.setText("<H1>an H1 Header</H1><br><P>First para<p>Second para<p>And the Third");
            add(jep);

  6. #6
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Make JTextArea Support HTML

    I'm glad you understood it.

    So you just put the "text/html" string label in the setContentType method?

    Do you think JEditorPane/JTextPane would recognize html coding from a text file when it got written to it?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,104
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Make JTextArea Support HTML

    What if you wanted to see the HTML text vs having it formatted?

  8. #8
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Make JTextArea Support HTML

    No I was hoping for having it formatted. I could just simply make HTML documents and load them in... but I would prefer it to be standard text with html coding in it and have JEditorPane/JTextPane recognize the coding when the file gets read and format it.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,104
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Make JTextArea Support HTML

    What would a browser do when it reads that type of file?

  10. #10
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Make JTextArea Support HTML

    Read it as a webpage? Or a URL?

    Or did you want me to try it? I will try it....

  11. #11
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Make JTextArea Support HTML

    It didn't work. I suppose the document has to be HTML then?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,104
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Make JTextArea Support HTML

    It didn't work.
    Can you explain?
    For example, I created a file that started with non html text and imbedded in it some html. I opened the file in a browser and the browser displayed ....

  13. #13
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Make JTextArea Support HTML

    Placing a html text in a standard text file didn't work for me when I viewed it in a web browser. It simply showed the html code in the text file.

    I did, however, get the JEditorPane to work with it. A BufferedReader reads lines from a text document with html code into a StringBuffer. Each appended line gets the "<br>" statement added on.

    The StringBuffer.toString() method is then used to set the JEditorPane's text to the StringBuffer's contents.

    The JEditorPane (after using the setContentType("text/html") method) shows the HTML code output successfully.

    Thanks for your help!

Similar Threads

  1. will java 1.3 support these APIs
    By HungryCoder in forum Java SE APIs
    Replies: 3
    Last Post: May 24th, 2011, 02:23 PM
  2. Re: Remote Desktop Support Application
    By viquarmca in forum Java Theory & Questions
    Replies: 1
    Last Post: December 16th, 2010, 02:45 PM
  3. Make JTextArea accept input from JButton
    By crazed8s in forum AWT / Java Swing
    Replies: 9
    Last Post: December 5th, 2010, 08:38 PM
  4. Replies: 1
    Last Post: September 30th, 2010, 02:36 PM
  5. Hibernate support for Oracle 11g
    By domminnik in forum JDBC & Databases
    Replies: 0
    Last Post: April 16th, 2010, 08:36 AM