Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...