Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.FileNotFoundException;

import javax.swing.JEditorPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

public class Main implements HyperlinkListener {
    private JEditorPane pane;

    public Main(JEditorPane jep) {
        pane = jep;
    }

    public void hyperlinkUpdate(HyperlinkEvent he) {
        HyperlinkEvent.EventType type = he.getEventType();
        if (type == HyperlinkEvent.EventType.ENTERED) {
            System.out.println(he.getURL().toString());
        } else if (type == HyperlinkEvent.EventType.EXITED) {
            System.out.println("Exited");
        } else if (type == HyperlinkEvent.EventType.ACTIVATED) {
            if (he instanceof HTMLFrameHyperlinkEvent) {
                HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) he;
                HTMLDocument doc = (HTMLDocument) pane.getDocument();
                doc.processHTMLFrameHyperlinkEvent(evt);
            } else {
                try {
                    pane.setPage(he.getURL());
                    System.out.println(he.getURL().toString());
                } catch (FileNotFoundException fnfe) {
                    pane.setText("Could not open file: <tt>" + he.getURL() + "</tt>.<hr>");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}