Java Swing Tutorial - Java JEditorPane .addHyperlinkListener (HyperlinkListener listener)








Syntax

JEditorPane.addHyperlinkListener(HyperlinkListener listener) has the following syntax.

public void addHyperlinkListener(HyperlinkListener listener)

Example

In the following code shows how to use JEditorPane.addHyperlinkListener(HyperlinkListener listener) method.

//from  w ww. jav  a 2  s .c  o m
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class Main extends JFrame implements ActionListener, HyperlinkListener {
  JEditorPane view;

  JTextField commandLine;

  Main(String title) {
    super(title);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    commandLine = new JTextField();
    commandLine.addActionListener(this);
    getContentPane().add(commandLine, BorderLayout.NORTH);

    view = new JEditorPane();
    view.setEditable(false);
    view.setPreferredSize(new Dimension(400, 400));
    view.addHyperlinkListener(this);

    getContentPane().add(view, BorderLayout.CENTER);

    pack();
    setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    try {
      URL url = new URL(e.getActionCommand());
      view.setPage(url);
      commandLine.setText(url.toExternalForm());
    } catch (MalformedURLException e2) {
      System.out.println("Bad URL: " + e.getActionCommand());
    } catch (java.io.IOException e2) {
    }
  }

  public void hyperlinkUpdate(HyperlinkEvent e) {
    try {
      view.setPage(e.getURL());
      commandLine.setText(e.getURL().toExternalForm());
    } catch (java.io.IOException e2) {
    }
  }

  public static void main(String[] args) {
    new Main("Editor Demo");
  }
}