Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Simple Java web browser


  • Please log in to reply
9 replies to this topic

#1 farrell2k

farrell2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 169 posts

Posted 29 June 2010 - 12:00 AM

A simple web browser in Java. It ONLY has support for reading HTML, and as such, anything but a pure HTML page is going to look strange. As I only spent about 10 minutes on this, don't expect hyper links to be navigable. :) I will probably update the tut to cover that someday. For now, you can type in a url and click 'go' to navigate there.

Here's what I did:

I created a JFrame.
I created a JEditorPane, which displays the HTML.
I created a JScrollPane and put the JEditorPane in it.
I created a JTextField which is used to enter urls.
I created a JButton which is used to call the 'setPage()' method of the JEditorPane.

The code is pretty easy to follow.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package browser;

/**
 *
 * @author Tom
 */

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

public class Browser {
    private JFrame frame;
    private JPanel panelTop;
    private JEditorPane editor;
    private JScrollPane scroll;
    private JTextField field;
    private JButton button;
    private URL url;

    public Browser(String title) {
        initComponents();

        //set the title of the frame
        frame.setTitle(title);

        //set the default cloe op of the jframe
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //set size of frame
        frame.setSize(800,600);

        //add jpanel to north of jframe
        frame.add(BorderLayout.NORTH, panelTop);

        //add textfield and navigation button to jpanel.
        panelTop.add(field);
        panelTop.add(button);

        //add scroll pane to jframe center
        frame.add(BorderLayout.CENTER, scroll);

        
        //set the frame visible
        frame.setVisible(true);
    }//end Browser() constructor

    private void initComponents() {
        //create the JFrame
        frame = new JFrame();

        //create the JPanel used to hold the text field and button.
        panelTop = new JPanel();
        
        //set the url
        try {
            url = new URL("http://www.javagaming.org");
        }
        catch(MalformedURLException mue) {
            JOptionPane.showMessageDialog(null,mue);
        }
        
        //create the JEditorPane
        try {
            editor = new JEditorPane(url);
            
            //set the editor pane to false.
            editor.setEditable(false);
        }
        catch(IOException ioe) {
            JOptionPane.showMessageDialog(null,ioe);
        }
        
        //create the scroll pane and add the JEditorPane to it.
        scroll = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        //create the JTextField
        field = new JTextField();

        //set the JTextField text to the url.
        //we're not doing this on the event dispatch thread, so we need to use
        //SwingUtilities.
        SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               field.setText(url.toString());
           }
        });

        //create the button for chanign pages.
        button = new JButton("Go");
        
        //add action listener to the button
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    editor.setPage(field.getText());
                }
                catch(IOException ioe) {
                    JOptionPane.showMessageDialog(null, ioe);
                }
            }
        });
    }//end initComponents()

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Browser("Simple web browser");
            }
        });
    }//end main method.
}//end Browser class
Any question, please ask.

Edited by farrell2k, 29 June 2010 - 04:20 PM.
Spelling correction.

  • 2

#2 farrell2k

farrell2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 169 posts

Posted 29 June 2010 - 05:37 PM

OK, so it navigating links is easier than I thought. To modify the above code to check for hyperlink clicks, we need to add a HyperLinkListener to the JEditorPane. Find the initComponents() method in the code above and add this:

editor.addHyperlinkListener(new HyperlinkListener() {
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
                       try {
                        editor.setPage(e.getURL());
                    }
                    catch(IOException ioe) {
                        JOptionPane.showMessageDialog(null,ioe);
                    }
            }//end hyperlinkUpdate()
        });//end HyperlinkListener
We add a HyperLinkListener to the JeditorPane referenced as editor, and then we override the HyperlinkListener interfaces hyperlinkUpdate() method. In this mehod we use an if statement to call e.getEventType() to see what happened, and if a hyperlink was clicked on, the event type is HyperlinkEvent.EventType.ACTIVATED, so we call the JEditorPane setUrl() method to change the url to the url of the hyperlink that was clicked by passing the argument e.getURL() to the JeditorPane's setPage() method. Easy.

The HyperlinkEvent.EventType class also has a field named 'ENTERED'. You could create a JLabel and add it to the JFrame and simply include another if statement in hyperLinkUpdate() that sets the text of the JLabel to the URL of the hyperlink your hovering. Many web browsers have this feature.
  • 0

#3 raghu

raghu

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 06 July 2010 - 09:50 AM

great dude...
  • 0

#4 gloomy

gloomy

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 11 July 2010 - 08:52 AM

Tested, works. Thank you.

It is nicely explained.

I posted compiled application to

Index of /java/Browser
  • 0

#5 Roman Y

Roman Y

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 196 posts

Posted 01 August 2010 - 06:34 AM

Thanks for the tutorial. Was pritty cool to try it out and try to modify it))
  • 0

#6 fyhring4

fyhring4

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 24 October 2010 - 02:33 AM

Awesome!
  • 0

#7 truongton92

truongton92

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 08 May 2015 - 12:34 AM

Thank you for sharing great article, but I hope to know more.
 
With your code to display the site in java is not correct, I want to have a program or how to display more accurate. My site contains video but with the code on it totally unenforceable
 
I hope java program segment displays it together with search engine user agent to assess hits to my site from these popular browsers like chrome, firefox
 
I hope there is a java program to access the website similar to the most popular browsers.
 
Thank you!

  • 0

#8 farrell2k

farrell2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 169 posts

Posted 10 July 2015 - 06:00 PM

 

Thank you for sharing great article, but I hope to know more.
 
With your code to display the site in java is not correct, I want to have a program or how to display more accurate. My site contains video but with the code on it totally unenforceable
 
I hope java program segment displays it together with search engine user agent to assess hits to my site from these popular browsers like chrome, firefox
 
I hope there is a java program to access the website similar to the most popular browsers.
 
Thank you!

 

 

It has been a while, but...

 

That's not going to happen with Swing.  You'll have to use JavaFX.   See my web browser tutorial here.


  • 0

Averageloser.com - I used to be a programmer like you, then I took a -> in the knee. 


#9 MrDeRp

MrDeRp

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 02 August 2016 - 02:46 PM

how can I have it work with google when ever I try to search something it does nothing


  • 0

#10 Michaelvok

Michaelvok

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 11 January 2017 - 10:00 PM

I tried the same and dont see anything bad happening, calling the disconnect in the thread that executes the event handler works just fine.
Can I see the code youre using?

Thanks

.
  • 0