Handle Hyperlink Event in JEditorPane - Java Swing

Java examples for Swing:JEditorPane

Introduction

The following code uses a lambda expression to add a HyperlinkListener to a JEditorPane.

import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.event.HyperlinkEvent;

public class Main {

  public static void main(String[] args) {
    try {
      JEditorPane editorPane = new JEditorPane("https://www.java2s.com");
      editorPane.addHyperlinkListener((HyperlinkEvent event) -> {
        if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          try {
            editorPane.setPage(event.getURL());
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      });

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Related Tutorials