Demonstrating the HyperlinkListener : Various Event Listener « Swing JFC « Java






Demonstrating the HyperlinkListener

Demonstrating the HyperlinkListener
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.IOException;

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

public class HyperlinkTest {
  public static void main(String args[]) {
      JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    final JEditorPane ep = new JEditorPane();

    try {
      ep.setPage("http://www.java2s.com");
    } catch (IOException e) {
      System.err.println("Bad URL: " + e);
      System.exit(-1);
    }

    HyperlinkListener listener = new HyperlinkListener() {
      public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          try {
            ep.setPage(e.getURL());
          } catch (IOException ioe) {
            System.err.println("Error loading: " + ioe);
          }
        }
      }
    };
    ep.addHyperlinkListener(listener);
    ep.setEditable(false);
    JScrollPane pane = new JScrollPane(ep);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(640, 480);
    frame.show();
  }
}
           
       








Related examples in the same category

1.Demonstrating the WindowListener with a WindowAdapterDemonstrating the WindowListener with a WindowAdapter
2.Demonstrating the ActionListenerDemonstrating the ActionListener
3.Demonstrating the AdjustmentListenerDemonstrating the AdjustmentListener
4.Demonstrating the AncestorListener
5.Demonstrating the ComponentListenerDemonstrating the ComponentListener
6.Demonstrating the ContainerListenerDemonstrating the ContainerListener
7.Demonstrating the FocusListenerDemonstrating the FocusListener
8.Demonstrating the InternalFrameListenerDemonstrating the InternalFrameListener
9.Demonstrating the ItemListenerDemonstrating the ItemListener
10.Demonstrating the KeyListenerDemonstrating the KeyListener
11.Demonstrating the MenuListenerDemonstrating the MenuListener
12.Demonstrating the MouseListener and MouseMotionListenerDemonstrating the MouseListener and MouseMotionListener
13.Demonstrating the MouseWheelListenerDemonstrating the MouseWheelListener
14.Demonstrating the PopupMenuListenerDemonstrating the PopupMenuListener
15.Demonstrating the WindowListener
16.Responding to KeystrokesResponding to Keystrokes