I have Java Desktop application that displays some information in a JTable
that may contain URLs with some text in some cells. How can I make only the URL click-able and allow the user to open it in a default browser if he/she clicks on it.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can use the approach shown here in a custom TableCellEditor
. Once selected, you can browse()
the URI.
Addendum: You can use JEditorPane
for your editor component and addHyperlinkListener()
to listen for events related to the link.
JEditorPane jep = new JEditorPane();
jep.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
HyperlinkEvent.EventType type = e.getEventType();
final URL url = e.getURL();
if (type == HyperlinkEvent.EventType.ENTERED) {
// do desired highlighting
} else if (type == HyperlinkEvent.EventType.ACTIVATED) {
// open browser
}
}
});
回答2:
here is a sample about displaying text as hyperlink: HyperLink in JTable Cell