-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHyperlink.java
More file actions
35 lines (29 loc) · 1015 Bytes
/
Copy pathHyperlink.java
File metadata and controls
35 lines (29 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package browser;
import java.io.IOException;
import java.net.URL;
import javax.swing.JEditorPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;
public class Hyperlink implements HyperlinkListener {
JEditorPane editorPane;
public Hyperlink(JEditorPane editorPane) {
this.editorPane = editorPane;
}
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
HyperlinkEvent.EventType type = hyperlinkEvent.getEventType();
final URL url = hyperlinkEvent.getURL();
if (type == HyperlinkEvent.EventType.ENTERED) {
System.out.println("URL: " + url);
} else if (type == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Activated");
Document doc = editorPane.getDocument();
try {
editorPane.setPage(url);
} catch (IOException ioException) {
System.out.println("Error following link");
editorPane.setDocument(doc);
}
}
}
}