Tuesday, 16 September 2014

A SIMPLE BROWSER USING JAVA

Main Class
import javax.swing.JFrame;


public class Main {
    public static void main(String args[]){
        ReadFile  dude =  new ReadFile();
        dude.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}






Html page fetch and display class -

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;


public class ReadFile extends JFrame {
    private JTextField addressBar; // address bar
    private JEditorPane display ; //  browser display pane
   
    // constructor
    public ReadFile(){
            // title for the browser window
        super("Jim's X-Browser");
       
        // initialise the guis
        addressBar = new JTextField("enter a URL hoss ! ") ;
        //add the action listner for the enter event by the user
        addressBar.addActionListener(
                new ActionListener(){
                   
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        //getActionCommand Returns the command
                        //string associated with this action.
                            loadCrap(e.getActionCommand());
                    }
                   
                }
                );
        //add to the scrreen
        add(addressBar , BorderLayout.NORTH);
       
        //set the display as JEditor pane used
        display = new JEditorPane();
        //can be used for creating notepad and stuff like that
        display.setEditable(false);
        display.addHyperlinkListener(new HyperlinkListener(){

            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                // TODO Auto-generated method stub
                 if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
                     loadCrap(e.getURL().toString());
               
            }
           
        }
    );
        add(new JScrollPane(display) ,  BorderLayout.CENTER);
        setSize(500,300);
        setVisible(true);
    }

   
    //load crap to display html file on screen
    private void loadCrap(String userTextURL){
        try{
            display.setPage(userTextURL);
            addressBar.setText(userTextURL);
        }catch( Exception e){
            System.out.println("crap !");
        }
       
   
}
}





No comments:

Post a Comment