-2

I am developing a software where the users can access any website through the clicking a java button.

JButton button1 = new JButton("Click Me");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent myEvent) {
    // Here clicking this method will open a website
   open("www.myrequiredWesite.com");
}

});

How can i call default browser from the above java source code so that the default browser will open the specific site clicking the button from java graphical user interface.

amee
  • 1

1 Answers1

-1
public static void openPage(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 openPage(URL url) {
    try {
        openPage(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

Note use java.net to use desktop object

Manikanta Reddy
  • 849
  • 9
  • 23