Solved

Displaying a HTML page in a JEditorPane

Posted on 2000-05-17
4
300 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

707 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

18 Experts available now in Live!

Get 1:1 Help Now