public class JEditorPaneLinkTest {
public static void main( String[] args ) {
SwingUtilities.invokeLater(() -> new JEditorPaneLinkTest().buildAndShowUI());
}
private void buildAndShowUI() {
JFrame frame = new JFrame("JEditorPaneLinkTest");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
String content = "Hier könnte ihr Text stehen<br/>\n" +
"<a href=\"http://www.google.de\">Das ist ein Link</a><br/>\n" +
"<a href=\"file:///test.txt\">Das ist ein Link zu einem File</a>";
JEditorPane contentArea = new JEditorPane();
contentArea.setEditorKit(new HTMLEditorKit());
contentArea.setEditable(false);
contentArea.addHyperlinkListener(e -> {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
System.out.println("Link klickt: " + e.getURL().toString());
}
});
contentArea.setText(content);
frame.add(new JScrollPane(contentArea));
frame.setVisible(true);
}
}