?
Solved

Displaying a HTML page in a JEditorPane

Posted on 2000-05-17
4
Medium Priority
?
324 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 30 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

830 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