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?
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.
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.
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);
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
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