Solved

Displaying a HTML page in a JEditorPane

Posted on 2000-05-17
4
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaFX TableView not displaying correctly 3 160
jmss example java 2 48
swing controls 2 37
batch vs regular insert in spring DAO 2 50
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

734 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