Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting java gui to open a webpage in web browser

I am trying to get a java gui to open a web page. So the gui runs some code that does things and then produces a html file. I then want this file to open in a web browser (preferrably Firefox) as soon as it is created. How would I go about doing that?

like image 663
The Special One Avatar asked Mar 02 '09 11:03

The Special One


People also ask

Can we use GUI in Java?

In Java applications, the components that comprise a GUI (Graphical User Interface) are stored in containers called forms. The Java language provides a set of user interface components from which GUI forms can be built.

What can you do with Java GUI?

GUI (Graphical User Interface) in Java is an easy-to-use visual experience builder for Java applications. It is mainly made of graphical components like buttons, labels, windows, etc. through which the user can interact with an application. GUI plays an important role to build easy interfaces for Java applications.


2 Answers

If you're using Java 6 or above, see the Desktop API, in particular browse. Use it like this (not tested):

// using this in real life, you'd probably want to check that the desktop // methods are supported using isDesktopSupported()...  String htmlFilePath = "path/to/html/file.html"; // path to your new file File htmlFile = new File(htmlFilePath);  // open the default web browser for the HTML page Desktop.getDesktop().browse(htmlFile.toURI());  // if a web browser is the default HTML handler, this might work too Desktop.getDesktop().open(htmlFile); 
like image 159
Dan Vinton Avatar answered Oct 05 '22 08:10

Dan Vinton


Ya, But if you want to open the webpage in your default web browser by a java program then you can try using this code.

/// file OpenPageInDefaultBrowser.java public class OpenPageInDefaultBrowser {    public static void main(String[] args) {        try {          //Set your page url in this string. For eg, I m using URL for Google Search engine          String url = "http://www.google.com";          java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));        }        catch (java.io.IOException e) {            System.out.println(e.getMessage());        }    } } /// End of file 
like image 24
Ankur Avatar answered Oct 05 '22 06:10

Ankur