Rectangle 27 0

swing Open a link in browser with java button?


public static void openWebpage(String urlString) {
    try {
        Desktop.getDesktop().browse(new URL(urlString).toURI());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Note
Rectangle 27 0

swing Open a link in browser with java button?


java.net
try {
    Desktop.getDesktop().browse(new URL("http://www.google.com").toURI());
} catch (Exception e) {}
Note
Rectangle 27 0

swing Open a link in browser with java button?


public static void openWebpage(URI uri) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(uri);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public static void openWebpage(URL url) {
    try {
        openWebpage(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

+1 for me too. Clear and direct.

@MountainX Debug and verify that the desktop is supported and that security implementations aren't restricting you from accessing the desktop instance. If you are running the JAR as an applet, security is the likely culprit.

@Vulcan--I'm not running the JAR as an applet. I'm not aware of any security settings that would prevent this from working. I "worked around" it by calling new ProcessBuilder("x-www-browser", uri.toString());. You would think that if there were security restrictions, the ProcessBuilder call would not work. But it does work. I have no idea why desktop.browse(uri) doesn't work, but I have seen that it doesn't work for a lot of people. I was guessing maybe it's a Netbeans issue, but I don't know.

Use the Desktop#browse(URI) method. It opens a URI in the user's default browser.

this doesn't appear to work in JAR files created by Netbeans 7.x. It works when the code is run from Netbeans, but not when deployed as a JAR file... at least in my experience. I'm still looking for a solution.

Note
Rectangle 27 0

swing Open a link in browser with java button?


Note
Rectangle 27 0

swing Open a link in browser with java button?


private void ButtonOpenWebActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try { 
         String url = "https://www.google.com";
         java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
       }
       catch (java.io.IOException e) {
           System.out.println(e.getMessage());
       }
    }
Note