Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to view a simple html page in JEditorPane.  RSS feed

 
rahul kumar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying this code pls correct it.but it gives this Exception and output
Output......
///////////////////////////////////////////////
text/plain
text/html
Exception in thread "main" java.net.MalformedURLException: no protocol: <html><b
ody><h1>W O W !.............</h1></body></html>
at java.net.URL.<init>(URL.java:579)
at java.net.URL.<init>(URL.java:476)
at java.net.URL.<init>(URL.java:425)
at javax.swing.JEditorPane.setPage(JEditorPane.java:774)
at JEditorPaneDemo.main(JEditorPaneDemo.java:17)
///////////////////////////////////////////////

Code....
///////////////////////////////////////////////
import javax.swing.*;
class JEditorPaneDemo
{
public static void main(String args[]) throws Exception
{
JFrame frm=new JFrame("EditorPane");
JEditorPane jdp=new JEditorPane();
String data="<html><body><h1>W O W !.............</h1></body></html>";
System.out.println(jdp.getContentType());
jdp.setContentType("text/html");
System.out.println(jdp.getContentType());
jdp.setPage(data);
frm.getContentPane().add(jdp);
frm.pack();
frm.setVisible(true);
}
}
///////////////////////////////////////////////
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul,
The problem is, that you are expecting setPage() to take a String and put the contents of that String into the JEditorPane. setPage(String url) expects the String to resolve into a properly formed URL and then attempts to load that resource over the network.
Hope this helps
Michael Morris
SCJP2
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The setPage() method requires a URL object or a String representation of a URL. You are using a String. Try the setText() method. It should work then. I corrected it below--I have not tested it.

Code....
///////////////////////////////////////////////
import javax.swing.*;
class JEditorPaneDemo
{
public static void main(String args[]) throws Exception
{
JFrame frm=new JFrame("EditorPane");
JEditorPane jdp=new JEditorPane();
String data="<html><body><h1>W O W !.............</h1></body></html>";
System.out.println(jdp.getContentType());
jdp.setContentType("text/html");
System.out.println(jdp.getContentType());
jdp.setText(data);
frm.getContentPane().add(jdp);
frm.pack();
frm.setVisible(true);
}
}
///////////////////////////////////////////////

HTH,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!