Solved

Displaying HTML content in a JTextArea

Posted on 2001-07-17
34
1,167 Views
Last Modified: 2008-02-01
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
Comment
Question by:k_suchdeva
  • 10
  • 10
  • 4
  • +5
34 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6292412
JTextArea does not support HTML.
For HTML support use JEditorPane.
0
 
LVL 92

Expert Comment

by:objects
ID: 6292416
JeditorPane pane = new JEditorPane("http://abc.com/some.html");
pane.setEditable(false);
0
 
LVL 92

Expert Comment

by:objects
ID: 6292419
Or:

pane.setContentType("text/html");
pane.setText("<html><body><b>abc</b></body></html.");
0
 
LVL 2

Author Comment

by:k_suchdeva
ID: 6292424
I have found it i can do it using JLabel from Sun Tutorial on JLabel.
0
 
LVL 92

Expert Comment

by:objects
ID: 6292439
Yep, JLabel also has (limited) html support.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6292516
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
 
LVL 92

Expert Comment

by:objects
ID: 6292522
Didn't I just say that :)
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6292540
Sorry, I have a "very good" connection here. Sorry again.
0
 
LVL 2

Author Comment

by:k_suchdeva
ID: 6292669
Ovi  and Objects
Please write some code displaying a full HTML page with images using the JEditorPane etc.
0
 
LVL 92

Expert Comment

by:objects
ID: 6292676
I already did :)

JeditorPane pane = new JEditorPane("http://abc.com/some.html");
pane.setEditable(false);
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6292738
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
 
LVL 16

Expert Comment

by:Valeri
ID: 6292900
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
 
LVL 92

Expert Comment

by:objects
ID: 6292912
That code won't work from an unsigned applet.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6292919
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
 
LVL 9

Expert Comment

by:Ovi
ID: 6292937
Who has said that is needed in an applet ? :)
0
 
LVL 92

Expert Comment

by:objects
ID: 6292946
No-one, but i'll bet that's what Valeri's problem is.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6292952
Maybe
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Expert Comment

by:Valeri
ID: 6293211
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
 
LVL 9

Expert Comment

by:Ovi
ID: 6293219
OK, with the first URL or the second ?
0
 
LVL 2

Author Comment

by:k_suchdeva
ID: 6293253
U must be behind a firewall.
Run using
java -Dhttp.proxyHost=host -Dhttp.proxyPort=port TestHTML
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6293287
For k_suchdeva :

Fits your needs ?
0
 
LVL 16

Expert Comment

by:Valeri
ID: 6293373
0
 
LVL 16

Expert Comment

by:Valeri
ID: 6293515
So,
How can I show html file that is placed at my local disk?

Best Regards,
Valeri
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6293523
like you do in your browser. Something like 'file:// ... path to your file'
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6378682
yo man :!
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6722394
0
 
LVL 27

Expert Comment

by:Asta Cu
ID: 6931745
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
 
LVL 5

Expert Comment

by:vemul
ID: 7669683
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
 
LVL 92

Expert Comment

by:objects
ID: 7670872
the questions been answered imo
0
 

Expert Comment

by:SpideyMod
ID: 7705992
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
 
LVL 92

Expert Comment

by:objects
ID: 7706139
I believe I first answered the original question :)
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 7706246
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
 

Expert Comment

by:SpideyMod
ID: 7706247
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
 
LVL 5

Expert Comment

by:Netminder
ID: 7714620
Closed.

Netminder
EE Admin
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now