Listen to hyper link event by JEditorPane in Java

Description

The following code shows how to listen to hyper link event by JEditorPane.

Example


/* w w w  . jav  a2 s  .c o  m*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.IDN;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class Main extends JFrame implements HyperlinkListener {
  private JTextField txtURL= new JTextField("");
  JEditorPane ep = new JEditorPane();
  private JLabel lblStatus= new JLabel(" ");

  public Main() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel pnlURL = new JPanel();
    pnlURL.setLayout(new BorderLayout());
    pnlURL.add(new JLabel("URL: "), BorderLayout.WEST);
    pnlURL.add(txtURL, BorderLayout.CENTER);
    getContentPane().add(pnlURL, BorderLayout.NORTH);
    getContentPane().add( ep, BorderLayout.CENTER);

    getContentPane().add(lblStatus, BorderLayout.SOUTH);

    ActionListener al = new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        try {
          String url = ae.getActionCommand().toLowerCase();
          if (url.startsWith("http://"))
            url = url.substring(7);
          ep.setPage("http://" + IDN.toASCII(url));
        } catch (Exception e) {
          e.printStackTrace();
          JOptionPane.showMessageDialog(Main.this, "Browser problem: " + e.getMessage());
        }
      }
    };
    txtURL.addActionListener(al);

    setSize(300, 300);
    setVisible(true);
  }
  public void hyperlinkUpdate(HyperlinkEvent hle) {
    HyperlinkEvent.EventType evtype = hle.getEventType();
    if (evtype == HyperlinkEvent.EventType.ENTERED)
      lblStatus.setText(hle.getURL().toString());
    else if (evtype == HyperlinkEvent.EventType.EXITED)
      lblStatus.setText(" ");
  }

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

The code above generates the following result.

Listen to hyper link event by JEditorPane in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer