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*;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);    }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: