Just dealt with an annoying topic: How to add a link to a Java swing frame.
It’s not that hard to create some blue labels, but it’s a bit tricky to call a browser browsing a specific website…
As I mentioned the problem is to call the users web browser. Since Java SE 6 they’ve added a new class called Desktop. With it you may interact with the users specific desktop. The call for a browser is more than simple, just tell the desktop to browse to an URL:
Unfortunately there isn’t support for every OS, before you could use it you should check if it is supported instead of falling into runtime errors..
So far.. But what if this technique isn’t supported!? Yeah, thats crappy ;)
You have to check which OS is being used, and decide what’s to do!
I searched a little bit through the Internet and developed the following solutions:
Combining these solutions, one could create a browse function. Extending the javax.swing.JLabel class, implementing java.awt.event.MouseListener and adding some more features (such as blue text, overloading some functions…) I developed a new class Link, see attachment.
Of course it is also attached, so feel free to use it on your own ;)
Unfortunately I’m one of these guys that don’t have a Mac, shame on me! So I just could test these technique for Win and Linux. If you are a proud owner of a different OS please test it and let me know whether it works or not.
If you have improvements please tell me also.
Leave a comment
There are multiple options to leave a comment: