Java/Network Protocol/Hyperlink

Материал из Java эксперт
Перейти к: навигация, поиск

How to change mouse cursor during mouse-over action on hyperlinks

 
 
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("exit");
    } 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 (Exception e) {
          e.printStackTrace();
        }
      }
    }
  }
}





Listening for Hyperlink Events from a JEditorPane Component

 

import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class Main {
  public static void main(String[] argv) throws Exception{
    String url = "http://java.sun.ru";
    JEditorPane editorPane = new JEditorPane(url);
    editorPane.setEditable(false);
    editorPane.addHyperlinkListener(new MyHyperlinkListener());
  }
}
class MyHyperlinkListener implements HyperlinkListener {
  public void hyperlinkUpdate(HyperlinkEvent evt) {
    if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      JEditorPane pane = (JEditorPane) evt.getSource();
      try {
        // Show the new page in the editor pane.
        pane.setPage(evt.getURL());
      } catch (IOException e) {
      }
    }
  }
}