How To Create A Web Browser In Java using Netbeans

by Didin J. on Oct 24, 2021 How To Create A Web Browser In Java using Netbeans

A comprehensive step by step tutorial on how to create a web simple web browser in Java using Apache Netbeans IDE

In this tutorial, we will show you how to create a web simple web browser in Java using Apache Netbeans IDE. This time, we will use Java Desktop using Swing Framework. This desktop application is a cross-platform application that can run on Microsoft Windows, Mac OS, Linux, etc. 

This tutorial is divided into several steps:

The flow of this simple web browser is very simple. There's a JFrame that contains JTextField, JEditorPane, JButton, and JPanel. The JTextField is used to enter the web address. JButton is used to refresh the web browser. JEditorPane is used to display the web page found after entering the web address. JPanel uses to align other components' positions or layouts.

We assume that you have downloaded and installed Apache Netbeans 12.4 and JDK 11. So, we can start this tutorial as per the steps above.

You can watch the video tutorial on our YouTube channel here.


Step #1: Create a New Java Application

To create a new Java application (Desktop Application), just start the Apache Netbeans 12.4. 

How To Create A Web Browser In Java using Netbeans - main netbeans

Click File -> New Project in the menu or click the box with the plus icon in the toolbar.

How To Create A Web Browser In Java using Netbeans - new project

Expand the Java with Ant in the categories then choose Java Application in the Projects then click the next button.

How To Create A Web Browser In Java using Netbeans - new java app

Fill the project name (ours: "MySimpleWebBrowser"), leave Project Location and Project Folder as defaults, uncheck the "User Dedicated Folder for Storing Libraries" checkbox, check "Create Main Class checkbox, and change the package name (ours: "com.djamware.MySimpleWebBrowser") then click the Finish button.

How To Create A Web Browser In Java using Netbeans - new project home

Now, we have a Java application created with a single main Java file inside the package.


Step #2: Add Required Swing Components

All required Swing components will be put in the JFrame. For that, create a new JFrame by right-clicking the Project name -> New -> JFrameForm.

How To Create A Web Browser In Java using Netbeans - new jframe

The new Frame Form dialog will look like this.

How To Create A Web Browser In Java using Netbeans - jframe form

Fill the name (ours: BrowserFrame), leave Project and Location as defaults, choose the package (ours: com.djamware), leave other fields as defaults then click the Finish button.

How To Create A Web Browser In Java using Netbeans - jframe design

Resize the JFrame by drag the bottom right of Frame to fit the window. Drag and drop a Panel from the Palette pane to the top left corner of the JFrame. 

How To Create A Web Browser In Java using Netbeans - jpanel in jframe

Expand the JPanel to fill the JFrame vertically.

How To Create A Web Browser In Java using Netbeans - resize jpanel

In the Properties pane -> Properties, find border properties then click the ellipsis button. 

How To Create A Web Browser In Java using Netbeans - palette

In the JPanel - border dialog, choose the Etched Border then click the OK button. Find the vertical size then set the value to 60. Make sure the Horizontal Resizable is checked. Switch to the Properties pane -> Code then change Variable Name to "topPanel". 

Next, drag and drop again the Panel then resize to fit the rest of the Frame area and make the border as the previous Panel. Change the Variable Name to "bottomPanel". So the whole JPanel in the JFrame looks like this.

How To Create A Web Browser In Java using Netbeans - final jpanel

Next, drag and drop the Button component in the palette pane to the left side inside the topPanel. Change the Variable Name to "refreshButton" and change the text to "Refresh".  

Next, drag and drop the Text Field from the palette pane to the topPanel right after the refresh button. Change the Variable Name to "addressField" and change the text to "Enter website address".

Next, drag and drop the Editor Pane from the palette pane to the bottomPanel. Resize the editor pane to fit the bottomPanel. Change the Variable Name to "browserPane". So, the whole JFrame look like this.

How To Create A Web Browser In Java using Netbeans - full components


Step #3: Make a Simple Web Browser Working

To make the simple web browser work, we need to write some codes inside the JFrame class. For that, switch the Design to Source mode in the BrowserForm pane. 

How To Create A Web Browser In Java using Netbeans - switch to source

Add a method to set the web page to browserPane and the web address text to addressField that is wrapped by the try-catch block.

    private void enterWebsite(String text) {
        try {
            browserPane.setPage(text);
            addressField.setText(text);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Invalid URL!");
        }
    }

The IOException that is caught will throw the message dialog if the URL from addressField is invalid.

Next, inside the BrowserForm method, add this event handler or action for addressField, browserPane, and refreshButton.

    public BrowserForm() {
        initComponents();

        addressField.addActionListener((ActionEvent event) -> {
            enterWebsite(event.getActionCommand());
        });
        
        browserPane.addHyperlinkListener((HyperlinkEvent event) -> {
            if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                enterWebsite(event.getURL().toString());
            }
        });
        
        refreshButton.addActionListener((ActionEvent event) -> {
            enterWebsite(addressField.getText());
        });
    }

We are using HyperlinkEvent to catch the typed URL from the addressField then transform it as a page to the browserPane. HyperlinkEvent is used to notify interested parties that something has happened with respect to a hypertext link.


Step #4: Run and Test the Java Web Browser

Before running this Java application, right-click the project name in the Project pane then choose properties.

How To Create A Web Browser In Java using Netbeans - Run netbeans app

From the Categories choose Run and change the Main Class to "com.djamware.BrowserForm" by clicking the browse button then choosing that class. Click the OK button.

Next, run this Java application by clicking the play button in the toolbar. Here is what a simple web browser looks like.

How To Create A Web Browser In Java using Netbeans - simple browser preview

Replace the Enter website address text with the valid URL then click Enter key. As you can see above, the website that you enter might not seem as expected because this is just a basic web browser which not compatible with modern web technologies.

That it's, How To Create A Web Browser In Java using Netbeans. You can get the full source codes from our GitHub.

That just the basic. If you need more deep learning about Java and Spring Framework you can take the following cheap course:

Thanks! 

Loading…