简体   繁体   中英

How to show URL as a click-able URL in Table and allow them to open in default browser?

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.

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
        }
    }
});

这里有一个关于将文本显示为超链接的示例: JTable Cell中的HyperLink

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM