+ Reply to Thread
Results 1 to 6 of 6

Thread: Simple Java web browser

  1. #1
    farrell2k is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    60
    Rep Power
    11

    Simple Java web browser

    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.

    Code:
    /*
     * 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.
    Last edited by farrell2k; 06-29-2010 at 05:20 PM. Reason: Spelling correction.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    farrell2k is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    60
    Rep Power
    11

    Re: Simple Java web browser

    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:

    Code:
    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.

  4. #3
    raghu is offline Newbie
    Join Date
    Jul 2009
    Location
    Hyderabad,India
    Posts
    11
    Rep Power
    0

    Re: Simple Java web browser

    great dude...

  5. #4
    gloomy's Avatar
    gloomy is offline Newbie
    Join Date
    Jul 2010
    Location
    B&H
    Posts
    13
    Rep Power
    0

    Re: Simple Java web browser

    Tested, works. Thank you.

    It is nicely explained.

    I posted compiled application to

    Index of /java/Browser

  6. #5
    Roman Y is offline Programmer
    Join Date
    Jul 2010
    Posts
    191
    Rep Power
    0

    Re: Simple Java web browser

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

  7. #6
    fyhring4 is offline Newbie
    Join Date
    Jan 2009
    Location
    Denmark
    Posts
    13
    Rep Power
    0

    Re: Simple Java web browser

    Awesome!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Creating A Simple Web Browser.
    By CommittedC0der in forum CSharp Tutorials
    Replies: 44
    Last Post: 12-29-2011, 01:36 AM
  2. HELP <<JAVA script Browser >>
    By quantlinear in forum Technology Ramble
    Replies: 1
    Last Post: 06-08-2011, 04:55 PM
  3. [ask] Java simple Game
    By linda_sarah in forum Java Help
    Replies: 2
    Last Post: 08-05-2010, 08:50 AM
  4. Simple do while <Java>
    By Turk4n in forum Classes and Code Snippets
    Replies: 0
    Last Post: 01-06-2010, 10:16 AM
  5. java Web Browser
    By markwilson in forum Java Help
    Replies: 13
    Last Post: 09-25-2008, 11:59 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts