Friday, June 12, 2015

Java - A Web Site Launcher Class

Here is a simple Java class that I wrote that will launch a default web browser:


// A simple and easy-to-use class that will launch a default browser.
// Noble D. Bell, June 5, 2015

public class LaunchWebBrowser {
 
 public String webUrl = ""; 
 
 // Initialize Object with data. If none is supplied then default to my web site.
 public LaunchWebBrowser(String url) {
  
  if (url.isEmpty())
   webUrl = "http://www.noblesoftware.com";
  else
   webUrl = url;
 }
 
 // Display the web site in the system's default web browser.
 public void GotoWebsite() {

  try {
   java.awt.Desktop.getDesktop().browse(java.net.URI. create(webUrl));
  } catch(java.io.IOException e) {
   javax.swing.JOptionPane.showMessageDialog(null, e.getStackTrace().toString());
  }
 }
}

No comments:

Post a Comment