I need to create a label with Java Swing that is clickable and able to open the default browser on the desktop and redirect it to a specific url. My code is able to open up the browser but not redirecting it to the correct url (the default home page is loaded). My test code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
public class LinkTest extends JFrame {
public LinkTest() {
JPanel p = new JPanel();
JLabel link = new JLabel("Click here");
link.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
link.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 0) {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
try {
URI uri = new URI("http://www.bbc.co.uk");
desktop.browse(uri);
} catch (IOException ex) {
ex.printStackTrace();
} catch (URISyntaxException ex) {
ex.printStackTrace();
}
}
}
}
});
p.add(link);
getContentPane().add(BorderLayout.NORTH, p);
}
public static void main(String[] args) {
LinkTest linkTest = new LinkTest();
linkTest.setSize(640,100);
linkTest.show();
}
}
How can I have a default browser open and redirect to the correct URL with Java Swing?
Easy, just copy this method to your code with the right parameters. Don't forget to add the imports needed.
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
private void goWebsite(JLabel website, final String url, String text) {
website.setText("<html> Website : <a href=\"\">"+text+"</a></html>");
website.setCursor(new Cursor(Cursor.HAND_CURSOR));
website.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
Desktop.getDesktop().browse(new URI(url));
} catch (URISyntaxException | IOException ex) {
//It looks like there's a problem
}
}
});
}
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 0) {
if (Desktop.isDesktopSupported()) {
try {
String osName = System.getProperty("os.name");
String urlPath = "http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html";
if (osName.startsWith("Windows"))
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + urlPath);
else {
String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++)
if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
browser = browsers[count];
Runtime.getRuntime().exec(new String[] { browser, urlPath });
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error in opening browser" + ":\n" + e.getLocalizedMessage());
}
}
}
}
});
Here is a code sample for what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With