• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1218
  • Last Modified:

Displaying HTML content in a JTextArea

How to program in Swing to display HTML style content.
I mean will it work ??          
JTextArea area=new JTextArea();
area.setText("<b>Hi hoooo ho hooo</b>");
I have heard that it is possible in Swing but do not know how to do it and the above approach does not work.
So basically I want to display some HTML style content in a JTextarea.
Thanx and Regards
Khem
0
k_suchdeva
Asked:
k_suchdeva
  • 10
  • 10
  • 4
  • +5
1 Solution
 
objectsCommented:
JTextArea does not support HTML.
For HTML support use JEditorPane.
0
 
objectsCommented:
JeditorPane pane = new JEditorPane("http://abc.com/some.html");
pane.setEditable(false);
0
 
objectsCommented:
Or:

pane.setContentType("text/html");
pane.setText("<html><body><b>abc</b></body></html.");
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
k_suchdevaAuthor Commented:
I have found it i can do it using JLabel from Sun Tutorial on JLabel.
0
 
objectsCommented:
Yep, JLabel also has (limited) html support.
0
 
OviCommented:
You must use JEditorPane or JTextPane for displaying html content setted with the HTMLEditorKit and HTMLDocument as defaults. after that you have there a method called setPage(URL) and of course setText()
0
 
objectsCommented:
Didn't I just say that :)
0
 
OviCommented:
Sorry, I have a "very good" connection here. Sorry again.
0
 
k_suchdevaAuthor Commented:
Ovi  and Objects
Please write some code displaying a full HTML page with images using the JEditorPane etc.
0
 
objectsCommented:
I already did :)

JeditorPane pane = new JEditorPane("http://abc.com/some.html");
pane.setEditable(false);
0
 
OviCommented:
import java.awt.*;
import javax.swing.*;

public class TestHTML {
  public static void main(String args[]) {
    JEditorPane pane = null;
    JFrame f = new JFrame();
    f.setSize(400, 400);
    f.setLocation(150, 100);
    f.getContentPane().setLayout(new BorderLayout());
    try {
      pane = new JEditorPane("http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20152577");
    } catch(Exception e) {}
    f.getContentPane().add(new JScrollPane(pane), BorderLayout.CENTER);
    f.setVisible(true);
  }
}

You must wait a little until the page is loaded.
0
 
ValeriCommented:
To Ovi :
Did you succeed to show the page in this way ?
Some security exceptions are shown at console...

catch(Exception e) {
 e.printStackTrace();
}

JEditorPane supports HTML3.2 and and lower.
I think something else should be added...

Best Regards
Valeri
   
0
 
objectsCommented:
That code won't work from an unsigned applet.
0
 
OviCommented:
on my computer is wotking fine (it loads the page) whitout any exceptions. I use JDK 1.3.1. Try to replace the url with www.altavista.com
0
 
OviCommented:
Who has said that is needed in an applet ? :)
0
 
objectsCommented:
No-one, but i'll bet that's what Valeri's problem is.
0
 
OviCommented:
Maybe
0
 
ValeriCommented:
I tried the proposed example with JDK 1.2:

AppAccelerator(tm) 1.2.010 for Java (JDK 1.2), x86 version.
Copyright (c) 1997-1999 Inprise Corporation. All Rights Reserved.
java.net.SocketException: Connection reset by peer: no further information
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(Compiled Code)
        at java.net.PlainSocketImpl.connectToAddress(Compiled Code)
        at java.net.PlainSocketImpl.connect(Compiled Code)
        at java.net.Socket.<init>(Compiled Code)
        at java.net.Socket.<init>(Compiled Code)
        at sun.net.NetworkClient.doConnect(Compiled Code)
        at sun.net.www.http.HttpClient.openServer(Compiled Code)
        at sun.net.www.http.HttpClient.openServer(Compiled Code)
        at sun.net.www.http.HttpClient.<init>(Compiled Code)
        at sun.net.www.http.HttpClient.<init>(Compiled Code)
        at sun.net.www.http.HttpClient.New(Compiled Code)
        at sun.net.www.protocol.http.HttpURLConnection.connect(Compiled Code)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Compiled C
ode)
        at javax.swing.JEditorPane.getStream(JEditorPane.java:485)
        at javax.swing.JEditorPane.setPage(JEditorPane.java:309)
        at javax.swing.JEditorPane.setPage(JEditorPane.java:555)
        at javax.swing.JEditorPane.<init>(JEditorPane.java:185)
        at TestHTML.main(TestHTML.java:41)

0
 
OviCommented:
OK, with the first URL or the second ?
0
 
k_suchdevaAuthor Commented:
U must be behind a firewall.
Run using
java -Dhttp.proxyHost=host -Dhttp.proxyPort=port TestHTML
0
 
OviCommented:
For k_suchdeva :

Fits your needs ?
0
 
ValeriCommented:
0
 
ValeriCommented:
So,
How can I show html file that is placed at my local disk?

Best Regards,
Valeri
0
 
OviCommented:
like you do in your browser. Something like 'file:// ... path to your file'
0
 
OviCommented:
yo man :!
0
 
Asta CuCommented:
Hopefully you've already been helped with this question, but thought you'd appreciate knowing this.

WindowsUpdate has new updates for .NET users; Details follow - Microsoft .NET Framework
The .NET Framework is a new feature of Windows. Applications built using the .NET Framework are more reliable and secure. You need to install the .NET Framework only if you have software that requires it.

For more information about the .NET Framework, see http://www.microsoft.com/net. (This site is in English.)

System Requirements
The .NET Framework can be installed on the following operating systems:
Windows 98
Windows 98 Second Edition (SE)
Windows Millennium Edition (Windows Me)
Windows NT 4.0® (Workstation or Server) with Service Pack 6.0a
Windows 2000 with the latest service pack installed (Professional, Server, Datacenter Server, or Advanced Server)
Windows XP (Home Edition and Professional)
You must be running Internet Explorer version 5.01 or later for all installations of the .NET Framework.

To install the .NET Framework, your computer must meet or exceed the following software and hardware requirements:

Software requirements for server operating systems:
MDAC 2.6
Hardware requirements:
For computers running only a .NET Framework application, Pentium 90 mHz CPU with 32 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
For server operating systems, Pentium 133 mHz CPU with 128 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
Recomended software:
MDAC 2.7 is recommended.
Recommended hardware: For computers running only a .NET Framework application, Pentium 90 MHz CPU with 96 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
For server operating systems, Pentium 133 MHz CPU with 256 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.

How to use -> Restart your computer to complete the installation. No other action is required to run .NET Framework applications. If you are developing applications using the .NET Framework, you can use the command-line compilers or you can use a development environment, such as Visual Studio .NET, that supports using the .NET Framework.

How to uninstall
To uninstall the .NET Framework: Click Start, point to Settings, and then click Control Panel (In Windows XP, click Start and then click Control Panel.).
Click Add/Remove Programs.
Click Microsoft .NET Framework (English) v1.0.3705 and then click Change/Remove.
More here  http://www.microsoft.com/net/

The .NET topic is being considered for addition to our All Topics link soon, so this may interest you as well:
http://www.experts-exchange.com/newtopics/Q.20276589.html

EXPERTS POINTS are waiting to be claimed here:  http://www.experts-exchange.com/commspt/Q.20277028.html

":0)
Asta


0
 
vemulCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- To be PAQ'ed and points NOT refunded
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 
objectsCommented:
the questions been answered imo
0
 
SpideyModCommented:
per recommendation.

SpideyMod
Community Support Moderator @Experts Exchange

objects,
Moderators cannot be expected to be knowledgeable in every area, it would be most helpful if you would provide an expert name for whom to assign the 100 points in your comments.  I have removed the points from this question but can issue a points for expertname question if if you post whom you think should get the points.  Thanks.
0
 
objectsCommented:
I believe I first answered the original question :)
0
 
SpideyModCommented:
objects,
Your points are here.  If you'll post there, I'll close tomorrow (Spidey needs his rest tonight, more crime to fight tomorrow).

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20452261.html
0
 
SpideyModCommented:
objects,
Your points are here.  If you'll post there, I'll close tomorrow (Spidey needs his rest tonight, more crime to fight tomorrow).

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20452261.html
0
 
NetminderCommented:
Closed.

Netminder
EE Admin
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 10
  • 10
  • 4
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now