Solved

Displaying a HTML page in a JEditorPane

Posted on 2000-05-17
4
306 Views
Last Modified: 2008-01-16
What do I have to do to display a HTML page in a JEditorPane?

This is what I am presently trying:

try
{
   browser.setPage(new URL(urlAddress.getText()));
   htmlDoc = (HTMLDocument)browser.getDocument();
}
catch(java.io.IOException ioe)
{}

Thanks,

Dirk
0
Comment
Question by:dirku
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
karthicraja earned 15 total points
ID: 2817880
Hope this code helps you.

Good luck
karthic.

import java.io.*;
import java.net.URL;
import javax.swing.*;
import java.net.MalformedURLException;

class HtmlPane extends JScrollPane
{
    /**
     * Editor pane object which renders the "text/HTML" on screen
     */
   
    private JEditorPane html;

   
    public HtmlPane(String _url)
    {
        try
        {
            URL url = new URL(_url);
            if(url != null)
                html = new JEditorPane(url);
              JViewport vp = getViewport();
            vp.add(html);
        }
        catch(MalformedURLException e)
        {
            System.out.println("Error framing the URL" + e.getMessage());
        }
        catch(IOException e)
        {
            System.out.println("File opening Error" + e.getMessage());
        }
    }
   
        public void setText(String str)
    {
        html.setText(str);
    }
}
  public class HtmlPane extends JFrame
  {
  public HtmlPane()
  {
    setBounds(200,25,500,400);
    Html htm = new Html();
    setContentPane(htm);
    }
    public static void main(String[] args)
    {
    HtmlPane hp = new HtmlPane();
    hp.show();
    }
    }
 
0
 

Author Comment

by:dirku
ID: 2820511
No, it doesn't work!
I've copied it and I became an IO-Error.
Besides you declare a public class HtmlPane and a non-public class HtmlPane in the same file which doesn't work.

However, is it important to a program when the access to the internet is given through a proxy server? Do I have to enter something different for instance than "http://www.yahoo.com"?
0
 
LVL 1

Expert Comment

by:karthicraja
ID: 2820573
Sorry in hurry i gave you a incomplete program use this.

import java.io.*;
import java.net.URL;
import javax.swing.*;
import java.net.MalformedURLException;

class Html extends JScrollPane
{
    /**
     * Editor pane object which renders the "text/HTML" on screen
     */
     
    private JEditorPane html;

   
    public Html(String _url)
    {
        try
        {
            URL url = new URL(_url);
            if(url != null)
                html = new JEditorPane(url);
            JViewport vp = getViewport();
            vp.add(html);
        }
        catch(MalformedURLException e)
        {
            System.out.println("Error framing the URL" + e.getMessage());
        }
        catch(IOException e)
        {
            System.out.println("File opening Error" + e.getMessage());
        }
    }
   
    public void setText(String str)
    {
        html.setText(str);
    }
}
public class HtmlPane extends JFrame
{
    public HtmlPane()
    {
        setBounds(200,25,500,400);
        Html htm = new Html("file:/d:/a.htm");
        setContentPane(htm);
    }
    public static void main(String[] args)
    {
        HtmlPane hp = new HtmlPane();
        hp.show();
    }
}
giving a file name This class will give you the URL use it if you need.
import java.net.URL;
import java.net.MalformedURLException;
import java.io.File;

public class URLCreator
{
      public static String getURLString(String strFile)
      {
            return getURL(strFile).toString();
      }
      
      public static URL getURL(String strFile)
      {
     URL url = null;
     try
     {
        url = new URL(strFile);
     }
     catch (MalformedURLException ex)
     {
        File f = new File(strFile);
        try
        {
           String path = f.getAbsolutePath();
           String fs = System.getProperty("file.separator");
           if (fs.length() == 1)
           {
              char sep = fs.charAt(0);
              if (sep != '/')
                 path = path.replace(sep, '/');
              if (path.charAt(0) != '/')
                 path = '/' + path;
           }
           path = "file://" + path;
           url = new URL(path);
        }
        catch (MalformedURLException e)
        {
           System.out.println("Cannot create url for: " + strFile);
        }
      }
      return url;
      }
}






0
 

Author Comment

by:dirku
ID: 2820762
Thanks, but it doesn't work with a proxy server which I have to handle.
Here's some code that works:

import java.net.*;
import java.io.*;
public class proxyURL {
    public static void main(String[] args) throws Exception {
        try {
            String authString = "userid:password";
            String auth = "Basic " + new sun.misc.BASE64Encoder().encode(authString.getBytes());
            URL url = new URL("http://java.sun.com/");
            URLConnection conn = url.openConnection();
            System.getProperties().put("proxySet", "true");
            System.getProperties().put("proxyHost", proxyHost);
            System.getProperties().put("proxyPort", proxyPort);
            conn.setRequestProperty("Proxy-Authorization", auth);
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                System.out.println(inputLine);
            in.close();
        } catch (Exception e) {}
    }
}

However, I'll give you the points (but then I have another question for free, OK?).

Regards,

Dirk
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error trying to install RTMT Win7 5 60
Spring Framework HTTPSession management 1 34
servlet  URL Rewriting 1 36
SHA2 certs for IIS AND Java? 2 88
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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.

773 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