Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2005

Tip: Looking for answers? Try searching our database.

Rendering HTML in a JTextPane

Thread view: 
Stefan Lintner - 24 Jun 2005 16:18 GMT
Hi,

is it possible to render a String containing HTML (e.g.
<h1>News</h1><p>News text</p>) in a JTextPane showing the String as
rendered HTML?

Thanks!
Thomas Fritsch - 24 Jun 2005 16:47 GMT
Stefan Lintner schrieb:
> Hi,
>
[quoted text clipped - 3 lines]
>
> Thanks!

Sure, it is as simple as this:

JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setText("<h1>News</h1><p>News text</p>");

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Stefan Lintner - 25 Jun 2005 08:27 GMT
Thomas Fritsch schrieb:
> Sure, it is as simple as this:
>
> JTextPane textPane = new JTextPane();
> textPane.setContentType("text/html");
> textPane.setText("<h1>News</h1><p>News text</p>");

Wow, thats easy :) Now I have URLs in the TextPane... However, they are
not clickable, the mouse cursor doesn't change. It would be great just
to call the default browser and send the URL if a URL is being clicked.

Can you give me a hint?

Thanks!
Stefan Lintner - 25 Jun 2005 09:26 GMT
Stefan Lintner schrieb:
> Wow, thats easy :) Now I have URLs in the TextPane... However, they are
> not clickable, the mouse cursor doesn't change. It would be great just
> to call the default browser and send the URL if a URL is being clicked.

I changed the JTextPane to a JEditorPane. The mouse cursor now changes
when hovering over a URL. But how can I launch a browser and direct it
to the clicked link?

Thanks!
Andrew Thompson - 25 Jun 2005 11:01 GMT
> I changed the JTextPane to a JEditorPane. The mouse cursor now changes
> when hovering over a URL. But how can I launch a browser

BrowserLauncher

>..and direct it to the clicked link?

HyperlinkListener

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Uwe Ziegenhagen - 27 Jun 2005 11:43 GMT
> Stefan Lintner schrieb:
>
[quoted text clipped - 8 lines]
>
> Thanks!

Look for 'Hyperactive' in the sun tutorials, they implement an easy
Listener.

Uwe

Signature

mail to newsgroup@ziegenhagen.info is read only from time to time. If
you need an urgent answer, google for me.

Frances - 02 Oct 2005 07:12 GMT
> Stefan Lintner schrieb:
>
[quoted text clipped - 11 lines]
> textPane.setContentType("text/html");
> textPane.setText("<h1>News</h1><p>News text</p>");

I am trying this right now..

 JTextPane textAreaTop;
 textAreaTop.setContentType("text/html");

get error:

 <identifier> expected
textAreaTop.setContentType("text/html");
                          ^

??   thank you..  Frances
Andrew Thompson - 02 Oct 2005 07:58 GMT
>> Stefan Lintner schrieb:
..
>>> is it possible to render a String containing HTML ...as
>>> rendered HTML?
..
>> Sure, it is as simple as this:
>>
>> JTextPane textPane = new JTextPane();
>> textPane.setContentType("text/html");
>> textPane.setText("<h1>News</h1><p>News text</p>");
...
> I am trying this right now..
>
[quoted text clipped - 8 lines]
>
> ??   thank you..  Frances

?? indeed.  Try this source..
<sscce>
import javax.swing.*;

public class TestContentType {

  public static void main(String[] args) {
    JTextPane textpane = new JTextPane();
    textpane.setContentType("text/html");
    textpane.setText("<h1>News</h1><p> Java Version: " +
      System.getProperty("java.version") + "</p>");
    JOptionPane.showMessageDialog(null, textpane);
  }
}
</sscce>

Here, that compiles and runs just fine, reporting..

"News
Java Version: 1.5.0-beta"
Roedy Green - 02 Oct 2005 08:56 GMT
> JTextPane textAreaTop;
>  textAreaTop.setContentType("text/html");

I converted your code to an SSCCE:

import javax.swing.JTextPane;

public class IdentifierExpected
  {
  /**
   * test harness
   *
   * @param args not used
   */
  public static void main ( String[] args )
     {
     JTextPane textAreaTop;
     textAreaTop = new JTextPane();
     textAreaTop.setContentType( "text/html" );
     }
  }

It compiles just fine. That means the problem is elsewhere in the code
that you did not show us.  Most likely you forgot or screwed up the
import.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Frances - 02 Oct 2005 23:21 GMT
>>JTextPane textAreaTop;
>> textAreaTop.setContentType("text/html");
[quoted text clipped - 21 lines]
> that you did not show us.  Most likely you forgot or screwed up the
> import.

Thank you Roedy.. of course it was a var scope problem... ;)
(code you posted compiles, but nothing opens when I run it..)  thank you
for your help Roedy...  Frances
Roedy Green - 03 Oct 2005 01:37 GMT
>Thank you Roedy.. of course it was a var scope problem... ;)
>(code you posted compiles, but nothing opens when I run it..)  thank you
>for your help Roedy...  Frances

You were complaining of a compile time problem, so all my SSCCE had to
do was compile.  I was not about to imaginatively write a whole mini
app for you that actually did something. Neither would you have had to
do that if all you were seeking help with was the compile problem.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Frances - 03 Oct 2005 03:19 GMT
>>Thank you Roedy.. of course it was a var scope problem... ;)
>>(code you posted compiles, but nothing opens when I run it..)  thank you
[quoted text clipped - 4 lines]
> app for you that actually did something. Neither would you have had to
> do that if all you were seeking help with was the compile problem.

ahhhhh...  ok...  sorry... :-<    thanks again for your help....
JavaByExample_at_KickJava_com@yahoo.com - 05 Oct 2005 12:27 GMT
> Sure, it is as simple as this:

> JTextPane textPane = new JTextPane();
> textPane.setContentType("text/html");
> textPane.setText("<h1>News</h1><p>News text</p>");

JTextPane is a subclass of JEditorPane, setContentType & setText are
methods of JEditorPane.

See more example about JEditorPane at http://kickjava.com/348.htm

David J
-----------------------------------------------------------------------------------------------------
http://KickJava.com - Java API By Example, From Geeks To Geeks.
http://KickJava.com/freeBooks.html - 100s of Free online Java Books.
http://KickJava.com/browse.html - Browse all J2SE, J2EE and J2ME APIs
and examples in one place.
http://kickjava.com/search.html - Searching 20,041,731 lines of Java
source codes.
http://KickJava.com/news - Daily Java news and articles, updated
continuously from 100+ sources


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.