devxlogo

Using the JEditorPane to Display HTML File

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);    }}
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist