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

后端 未结 2 839
一整个雨季
一整个雨季 2020-12-11 08:18

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

相关标签:
2条回答
  • 2020-12-11 08:41

    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
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-11 08:50

    here is a sample about displaying text as hyperlink: HyperLink in JTable Cell

    0 讨论(0)
提交回复
热议问题