Simple Web Browser in Java
Submitted by mehfuza on Sunday, September 8, 2013 - 18:16.
In this tutorial we will see how to create a simple web browser. It contains an address bar where a user can type the address and a go button which when clicked loads the web page. For displaying the web pages we require JEditor Pane and HyperlinkListener is required to respond when the user clicks on a link in the document. Following are the steps for creating the web browser.
Step 1: Defining the constructor of the class
The main class extends the JPanel. First we will set it's background color and layout as a Border Layout ans set border for it as follows.
Scroll Pane is added to the edit pane and hyperlink listener is registered with this class. Edit Pane is placed in the center of the panel.
Next a toolbar is created and added to the north of the panel. A label, text field for location input and go button are added to this tool bar. Action Listener is added to the go button and location input field to load the url.
Step 2: Implementing Hyperlink Listener
Hyperlink Listener contains a single function as hyperlinkUpdate() which is defined as follows. If hyperlink is clicked then URL of that hyperlink is loaded in the edit pane by calling loadURL() method.
Step 3: Defining loadURL() method
It loads the document at the specified URL into the edit pane. try catch block is used to handle errors if occurred any while changing the contents of the edit pane. JEditPane's setPage() method is used to set the edit pane's content.
Step 5: main() for defining different properties of frame
A frame is created and object of the main class(SimpleWebBrowser) is set as content of the frame as it is a panel. Location of the frame is centered using Toolkit as follows.
- editPane.setEditable(false);
- editPane.addHyperlinkListener(new LinkListener());
- toolbar.setFloatable(false);
- locationInput.addActionListener(goListener);
- goButton.addActionListener(goListener);
- toolbar.add(locationInput);
- toolbar.add(goButton);
- loadURL(evt.getURL());
- }
Step 4: ActionListener for Go button and Location input field
Here the location input field's data is stored in a string variable and checked whether its format is wrong or right. If the format is correct then URL is created and loadURL() method is called to load that URL else suitable message is displayed.
- URL url;
- try {
- if (location.length() == 0) //empty URL
- if (! location.contains("://")) // "://" if it not present in the string
- location = "http://" + location;
- }
- "The Location input box does not\nccontain a legal URL.");
- return;
- }
- loadURL(url);
- locationInput.selectAll();
- locationInput.requestFocus();
- }
- SimpleWebBrowser content = new SimpleWebBrowser();
- window.setContentPane(content);
- window.setSize(600,500);
- window.setLocation( (screenSize.width - window.getWidth())/2, (screenSize.height - window.getHeight())/2 );//center the frame
- window.setVisible(true);
- }
Comments
Add new comment
- Add new comment
- 6087 views