Chapter 8. Creating a Custom Web Browser in Java: The Browser ProjectThe 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:
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. |