HyperTextPane
基于@GuillaumePolet 代码的单个类 ( ):
package stackoverflow;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class Main {
static void showUI() {
javax.swing.SwingUtilities.invokeLater(
() -> {
JFrame frame = new JFrame();
HyperTextPane label = new HyperTextPane("<html>Visit new: <a href=\"http://www.google.pl\">google</a><br>" +
"Visit <a href=\"http://stackoverflow.com\">stackoverflow</a>" +
"</html>");
label.setHyperlinkClickListener(
(e,href,button) -> {
if (button == MouseEvent.BUTTON1) {
HyperTextPane.openHyperlink(href);
} else if (button == MouseEvent.BUTTON3) {
label.showHyperlinkCopyMenu(e,href);
}
});
frame.add(label);
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
);
}
public static void main(String[] args) {
showUI();
}
}
class HyperTextPane extends JTextPane {
HyperlinkClickListener hyperlinkClickListener;
public HyperTextPane(String htmlText) {
setContentType("text/html");
setEditable(false);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Element h = getHyperlinkElement(e);
if (h != null) {
Object attribute = h.getAttributes().getAttribute(HTML.Tag.A);
if (attribute instanceof AttributeSet) {
AttributeSet set = (AttributeSet) attribute;
String href = (String) set.getAttribute(HTML.Attribute.HREF);
if (href != null) {
hyperlinkClickListener.onHyperlinkClicked(h, href, e.getButton());
}
}
}
}
});
setText(htmlText);
}
public void setHyperlinkClickListener(HyperlinkClickListener hyperlinkClickListener) {
this.hyperlinkClickListener = hyperlinkClickListener;
}
private Element getHyperlinkElement(MouseEvent event) {
JEditorPane editor = (JEditorPane) event.getSource();
int pos = editor.getUI().viewToModel(editor, event.getPoint());
if (pos >= 0 && editor.getDocument() instanceof HTMLDocument) {
HTMLDocument hdoc = (HTMLDocument) editor.getDocument();
Element elem = hdoc.getCharacterElement(pos);
if (elem.getAttributes().getAttribute(HTML.Tag.A) != null) {
return elem;
}
}
return null;
}
public static boolean openHyperlink(String href) {
try {
Desktop.getDesktop().browse(new URI(href));
return true;
} catch (IOException | URISyntaxException e1) {
e1.printStackTrace();
}
return false;
}
public void showHyperlinkCopyMenu(Element elem, String href) {
JPopupMenu popup = new JPopupMenu();
popup.add("Copy URL");
((JMenuItem)popup.getComponent(0)).addActionListener(e -> {
StringSelection selection = new StringSelection(href);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection); }
);
try {
Rectangle rec = modelToView(elem.getStartOffset());
popup.show(this, rec.x, rec.y+rec.height);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
public interface HyperlinkClickListener {
void onHyperlinkClicked(Element element, String href, int mouseButton);
}
}
此示例还显示在默认浏览器中打开超链接,并将超链接地址复制到剪贴板。