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.
Asked
Active
Viewed 3,182 times
4

mKorbel
- 109,525
- 20
- 134
- 319

Ashish Pancholi
- 4,569
- 13
- 50
- 88
2 Answers
5
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
}
}
});
-
Thanks but I have two issue with that my JTable is editable and I have text with URL links in the same cell. I would like to show text as-is (as a simple text i.e. unclickable) while like to open URL in browser. – Ashish Pancholi Jan 27 '12 at 09:20
2
here is a sample about displaying text as hyperlink: HyperLink in JTable Cell

Waqas
- 6,812
- 2
- 33
- 50
-
It is odd that example has the call to `Desktop.getDesktop().browse(url.toURI());` commented out! I wonder what the logic is? – Andrew Thompson Jan 27 '12 at 06:11
-
-
I can see the appeal of using the renderer to simulate a roll-over effect, but I think the combination of renderer & editor is more flexible. Have you run the example cited? – trashgod Jan 27 '12 at 06:30
-