Solved

Displaying a HTML page in a JEditorPane

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

695 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