How to show the url as a clickable url in the table and allow them to open the default browser?

I have a Java Desktop application that displays some information in a JTable , which may contain URLs with some text in some cells. How can I make only a URL click and allow the user to open it in the default browser if he clicks on it.

+4
source share
2 answers

You can use the proposed approach here in the custom TableCellEditor selecting, you can browse() URI.

Application: you can use JEditorPane for your editor component and addHyperlinkListener() to listen for link related events.

 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 } } }); 
+5
source

here is an example of displaying text as a hyperlink: HyperLink in JTable Cell

+2
source

Source: https://habr.com/ru/post/1393305/


All Articles