Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Feb 10, 2003

Using the JEditorPane to Display HTML File


The JEditorPane provides a mechanism through which you can add support for custom text formats. A standard editor pane can understand plain text, HTML, and RTF.
 
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.net.*;
class JEditorPanes extends JFrame implements HyperlinkListener
{
    URL url;
    JEditorPane editPane;
    JEditorPanes(String path)throws Exception
    {
        Container c=getContentPane();
        editPane=new JEditorPane();// 
Creating an Instance for JEditorPane
        JScrollPane scrollPane = new JScrollPane(editPane,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//JEditorPane is 
added to the JScrollBar
        editPane.addHyperlinkListener(this);//To trap Hyper 
Links clicked by the user HyperlinkListener is added
        editPane.setEditable(false);//Editor pane 
is made Read Only
        url=new URL("file:"+path);
        editPane.setPage(url);
        c.add(scrollPane);
        pack();
    }
    public void hyperlinkUpdate(HyperlinkEvent hy)
    {
    //To trap Hyper Link Clicked by user.
        if (hy.getEventType()==hy.EventType.ACTIVATED)
        {
            try{
            editPane.setPage(hy.getURL());
            }catch(Exception x)
            {
                System.out.println(x);
            }
        }
    }
    static void main(String[]arr)throws Exception
    {
        JEditorPanes pan=new JEditorPanes(arr[0]);
        pan.setVisible(true);
    }
}
M.V. Prem
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date