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.

August 24th, 2011, 08:09 PM

Norm

Re: Make JTextArea Support HTML

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

August 24th, 2011, 10:54 PM

Junky

Re: Make JTextArea Support HTML

Quote:

Originally Posted by bgroenks96

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?

August 26th, 2011, 02:14 PM

bgroenks96

Re: Make JTextArea Support HTML

Quote:

Originally Posted by Junky

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.

August 26th, 2011, 02:18 PM

Norm

Re: Make JTextArea Support HTML

After looking at the API doc for the JEditorPane for a bit I tried this:

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?

August 27th, 2011, 07:50 AM

Norm

Re: Make JTextArea Support HTML

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

August 27th, 2011, 11:22 AM

bgroenks96

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.

August 27th, 2011, 11:24 AM

Norm

Re: Make JTextArea Support HTML

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

August 27th, 2011, 10:48 PM

bgroenks96

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....

August 27th, 2011, 10:50 PM

bgroenks96

Re: Make JTextArea Support HTML

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

August 28th, 2011, 07:11 AM

Norm

Re: Make JTextArea Support HTML

Quote:

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 ....

August 28th, 2011, 01:01 PM

bgroenks96

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.