Chapter8.Creating a Custom Web Browser in Java: The Browser Project


Chapter 8. Creating a Custom Web Browser in Java: The Browser Project

The Browser project.

The kinds of GUIs you can build in Java are extensive and have plenty of capability built in. But one of the things that isn't built in is an easy way to display a web browser. In larger projects for use by the general public, I've found including a browser can be very useful, especially for letting the user browse documentation by automatically navigating the browser to the right page, whether that page is online or local. If you've got lots of online help documentation and you want to let the user click a help topic and see that topic displayed, using a browser as a built-in control is perfect. Same goes for automatically registering the user's copy of your program online, filling out feedback forms, letting the user ask tech support questions, and much more. In fact, a built-in browser control is great for any kind of image and text handling, whether local or on the Web. It's an asset in any application that can impress the heck out of people.

How can you get a browser control that you can use in Java? Java comes with some built-in support for connecting to web servers, so you could try writing such a control yourself.

For example, say you were trying to display the home page of Sun Microsystems, the developers of Java. You could use the URLConnection class to open a URL connection to the Sun server, asking for the page you want (in this case, http://www.sun.com/index.html) in a project named, say, TextBrowser.java:

 import java.io.*; import java.net.*; class TextBrowser {     public static void main(String args[]) throws Exception     {         URL url = new URL("http://www.sun.com/index.html");         URLConnection urlconnection = url.openConnection();         .         .         . } 

Then you could open an InputStream from the server, reading characters one by one and displaying them:

 import java.io.*; import java.net.*; class TextBrowser {     public static void main(String args[]) throws Exception     {         int character;         URL url = new URL("http://www.sun.com/index.html");         URLConnection urlconnection = url.openConnection();         InputStream in = urlconnection.getInputStream();         while ((character = in.read()) != -1) {             System.out.print((char) character);         }         in.close();     } } 

This actually works, and here's the output you get as of this writing:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title> Sun Microsystems </title> <meta name="keywords" content="sun microsystems, sun, java, java computing, solaris, sparc, unix, jini, computer systems, server, mission critical, RAS, high availability, cluster, workgroup server, desktop, workstation, storage, backup solutions, network computer, network computing, hardware, software, service, consulting, support, training, compiler, jdk, technical computing, scientific computing, high performance, enterprise computing, staroffice, starportal, sun ray"> <meta name="description" content="Sun Microsystems, Inc. The Network Is The Computer™."> <meta http-equiv="pics-label" content='(pics-1.1 "http://www.icra.org/ratingsv02.html" l gen true for "http://www.sun.com" r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true for "http://www.sun.com" r (n 0 s 0 v 0 l 0))'> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">         .         .         . 

Swell, but no user is going to want to see 20 pages of untreated HTML. What about displaying images and formatted text as you'd see in a standard browser?

No problem. Just download the Standard Widget Toolkit (SWT), which you can get for free with the Eclipse Java Integrated Development Environment (a superb tool in its own right). Using the SWT, you can create a browser control easily, and it'll give you many of the full features available in a browser in your Java programs, as you see in Figure 8.1, which shows the Browser project at workdisplaying the Sun Microsystems home page, in fact.

Figure 8.1. The Custom Browser project.


Just as you'd expect in a real browser, the Browser project features these buttons:

  • Go Go to the URL in the text box.

  • Forward Go forward one page in the browser's history.

  • Back Go back one page in the browser's history.

  • Refresh Refresh the page.

  • Stop Stop all operations.

REAL-WORLD SCENARIO: Why SWT?

Developers have always had some issues with the GUI toolkits that come with Java. The Abstract Windowing Toolkit, AWT, was Sun's first version of a GUI toolkit for Java, and it was created hastily. So hastily, in fact, that it used the underlying operating system's GUI elements instead of its own. In Windows, you'd see a Windows button. On the Mac, you'd see a Mac button. This limited Sun to only those controls that were common to all operating systems that supported Java, and in consequence, the AWT was very limited.

Next came Swing, with a set of built-in Java controls. This set of Java-specific controls gave Java applications a common look across multiple platforms, but it also didn't let you develop applications that looked like they were designed for Windows, the Mac, and so on. To get around that, Sun added a "look-and-feel" emulation that lets your applications look like they were designed for the operating system they're running on. Unfortunately, the "look and feel" has lagged behind the new versions of many operating systems that have appeared. And Swing controls, written in Java, can be a great deal slower than the native controls in an operating system.

The SWT addresses these problems, and it makes developers happier by offering them a set of widgets that use native controls when they're available (for speed and visual compatibility with other programs). If such controls aren't available, the SWT adds them using code written and compiled for the operating system being used (not in Java).

The SWT is steadily gaining ground, especially because it comes built in to the very popular Eclipse Integrated Development Environment. Watch for the SWT to make a substantial impact in the coming years.


NOTE

The SWT doesn't actually create a browser from scratch, including full support for graphics, JavaScript, Flash, and so on built in. It uses Internet Explorer, and that's the Internet Explorer you see in Figure 8.1. You'll need IE installed on your system before you can use SWT to create your own browser controls.




    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net