How to Make a Web Browser

How to Make a Web Browser thumbnail
Java Programming Language Logo

Many beginning programmers believe that creating a Web browser can only be done in a large, group-project setting with hundreds of thousands of line of programming code required to accomplish the task. Although this is true to create a Web browser that fully complies with all of the Web Consortium's conformance recommendations, you can create a basic Web browser using Java Swing in a short time-frame. This basic browser allows you to navigate to Web pages, view images, link to other pages and save pages, but will not do things such as load flash applets and other features that require browser plug-ins to be installed.

Things You'll Need

  • Internet access
  • Java development environment
Show More

Instructions

    • 1

      Download and install the latest version of the Java standard developer's kit (SDK) from Sun Microsystems if you have not previously done so.

    • 2

      Open your programming application or text editor and save the file as myFirstWebBrowser.java. Include at the top of the file four class library imports (java.awt, java.io, java.awt.event, and javax.swing). These libraries are necessary to create the user interface components for the application (awt and swint), conducting HTML get and put commands (to view Web pages), and to save files (io). The import statements to include are:

      import java.awt.*;
      import java.io.*;
      import java.awt.event.*;
      import javax.swing.*;

    • 3

      Create the new class called myFirstWebBrowser, which will be an extension of JInternalFrame so that you can include the Web browser as an internal frame in a Java Swing application. The beginning of the class definition is:

      public class PageFrame extends JInternalFrame implements ActionListener {

    • 4

      Create class definitions for a SiteManager class, a String for the filename to load and a textArea. The constructor of the class will load the site name and SiteManager objects to assign to the protected class variables. The code to do these steps is:

      SiteManager myParent;
      String myFilename;
      JTextArea myTa;

      public PageFrame(String myName, SiteManager mySm) {
      super("Page: " + myName, true, true, true, true);
      myParent = mySm;
      setBounds(50,50,300,150);

      Container contentPane = getContentPane();

    • 5

      Create the text area that will display the HTML file and load it into a scrollable Swing pane that will allow you to view the entire HTML file. After these steps are complete, create a menu bar for the application to mimic the File menus that you are used to having in other computer applications. In this example, "File" and "Save" menu options will be created. The code to do these steps is:

      myTa = new JTextArea();
      JScrollPane myJsp = new JScrollPane(ta);
      contentPane.add(jsp, BorderLayout.CENTER);

      JMenuBar myJmb = new JMenuBar();
      JMenu myfileMenu = new JMenu("File");
      JMenuItem mySaveItem = new JMenuItem("Save");
      mySaveItem.addActionListener(this);
      myFileMenu.add(saveItem);
      myJmb.add(fileMenu);
      setJMenuBar(myJmb);

      myFilename = myName;
      loadContent();
      }

    • 6

      Define "Action Listeners" that will perform method calls on response to the user choosing "File->Menu" options to load and save files. The code to do this is:
      public void actionPerformed(ActionEvent ae) {

      saveContent();
      }

      public void loadContent() {
      try {
      FileReader myFr = new FileReader(myFilename);
      myTa.read(myFr, null);
      myFr.close();
      } catch (Exception e) {
      System.err.println("Could not load the web page: " + myFilename);
      }
      }

      public void saveContent() {
      try {
      FileWriter myFw = new FileWriter(myFilename);
      myTa.write(myFw);
      myFw.close();
      } catch(Exception e) {
      System.err.println("Could not save the webpage: " + myFilename);
      }
      }

      }

Tips & Warnings

  • The LoboBrowser (see Resources below) is an open-source, Java-based Web browser attempting to conform to all W3C recommendations that you can use without charge for larger projects requiring a tailor-made Web browser.

Related Searches:

Resources

  • Photo Credit WIkimedia Commons by Sun Microsystems

Comments

You May Also Like

Related Ads

Featured