Results 1 to 9 of 9
  1. #1
    prof.deedee is offline Member
    Join Date
    Oct 2009
    Posts
    36
    Rep Power
    0

    Default understand the code

    Hi
    I found this code in the Internet and I think it will help me to create simple web browser :eek::eek::eek:but because I am new in Java I can't understand everything in it ....... can anyone help me???D:D

    Java Code:
    import javax.swing.text.html.*;
    import javax.swing.text.*;
    import javax.swing.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    /**
       A simple Web Browser with minimal functionality.
       @author Jose M. Vidal
    */
    public class Browser {
    
    	/** Set the page.
    		@param jep the pane on which to display the url
    		@param url the url to display */
     	protected static void setPage(JEditorPane jep, String url){
    		try {
    			jep.setPage(url);
    		}
    		catch (IOException e) {
    			System.err.println(e);
    			System.exit(-1);
    		}
    
    	}
    
    	/** An inner class which listens for keypresses on the Back button. */
    	class backButtonListener implements ActionListener {
    		protected JEditorPane jep;
    		protected JLabel label;
    		protected JButton backButton;
    		protected Vector history;
    		public backButtonListener(JEditorPane jep, JButton backButton, Vector history, JLabel label){
    			this.jep = jep;
    			this.backButton = backButton;
    			this.history = history;
    			this.label = label;
    		}
    
    		/** The action is to show the last url in the history.
    		 @param e the event*/
    		public void actionPerformed(ActionEvent e){
    			try{
    				//the current page is the last, remove it
    				String curl = (String)history.lastElement();
    				history.removeElement(curl);
    					
    				curl = (String)history.lastElement();
    				System.out.println("Back to " + curl);
    				setPage(jep,curl);
    				label.setText("<html><b>URL:</b> "+ curl);
    				if (history.size() == 1)
    					backButton.setEnabled(false);
    			}
    			catch (Exception ex){
    				System.out.println("Exception " + ex);
    			}
    		}
    	}
    
    	/** An inner class that listens for hyperlinkEvent.*/
    	class LinkFollower implements HyperlinkListener {
    		protected JEditorPane jep;
    		protected JLabel label;
    		protected JButton backButton;
    		protected Vector history;
    		public LinkFollower(JEditorPane jep, JButton backButton, Vector history, JLabel label){
    			this.jep = jep;
    			this.backButton = backButton; 
    			this.history = history;
    			this.label = label;
    		}
    		/** The action is to show the page of the URL the user clicked on.
    			@param evt the event. We only care when its type is ACTIVATED. */
    		public void hyperlinkUpdate(HyperlinkEvent evt){
    			if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
    				try {
    					String currentURL = evt.getURL().toString();
    					history.add(currentURL);
    					backButton.setEnabled(true);
    					System.out.println("Going to " + currentURL);
    					setPage(jep,currentURL);
    					label.setText("<html><b>URL:</b> "+ currentURL);
    				}
    				catch (Exception e) {
    					System.out.println("ERROR: Trouble fetching url");
    				}
    			}
    		}
    
    	}
    
    	/** The contructor runs the browser. It displays the main frame with the
    		fetched initialPage
    		@param initialPage the first page to show */
     	public Browser(String initialPage){
    
    		/** A vector of String containing the past urls */
    		Vector history = new Vector();
    		history.add(initialPage);
    		
    		// set up the editor pane
    		JEditorPane jep = new JEditorPane();
    		jep.setEditable(false);
    		setPage(jep, initialPage);
    
    		// set up the window
    		JScrollPane scrollPane = new JScrollPane(jep);     
    		JFrame f = new JFrame("Simple Web Browser");
    		f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    
    		//Exit the program when user closes window.
    		f.addWindowListener(new WindowAdapter() {
    				public void windowClosing(WindowEvent e){
    					System.exit(0);
    				}
    			});
    
    		//Label where we show the url
    		JLabel label = new JLabel("<html><b>URL:</b> "+ initialPage);
    
    		
    		JButton backButton = new JButton ("Back");
    		backButton.setActionCommand("back");
    		backButton.setToolTipText("Go to previous page");
    		backButton.setEnabled(false);
    		backButton.addActionListener(new backButtonListener(jep, backButton, history, label));
    
    		JButton exitButton = new JButton ("Exit");
    		exitButton.setActionCommand("exit");
    		exitButton.setToolTipText("Quit this application");
    		exitButton.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					System.exit(0);
    				}
    			});
    
    		//A toolbar to hold all our buttons
    		JToolBar toolBar = new JToolBar();
    		toolBar.add(backButton);
    		toolBar.add(exitButton);
    
    
    		jep.addHyperlinkListener(new LinkFollower(jep, backButton, history, label));
    
    		//Set up the toolbar and scrollbar in the contentpane of the frame
    		JPanel contentPane = (JPanel)f.getContentPane();
    		contentPane.setLayout(new BorderLayout());
    		contentPane.setPreferredSize(new Dimension(400, 100));
    		contentPane.add(toolBar, BorderLayout.NORTH);
    		contentPane.add(scrollPane, BorderLayout.CENTER);
    		contentPane.add(label, BorderLayout.SOUTH);
    
    		f.pack();
    		f.setSize(640, 360);
    		f.setVisible(true);
    
    
    	}
    
    	/** Create a Browser object. Use the command-line url if given */
    	public static void main(String[] args) {
    		String initialPage = new String("URL");//URL=any url u like
    
    		if (args.length > 0) initialPage = args[0];
    
    		Browser b = new Browser(initialPage);
    	}
    	
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    What exactly do you not understand? Everything?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by prof.deedee View Post
    I found this code in the Internet and I think it will help me to create simple web browser
    You're lucky then because you have found one.

    kind regards,

    Jos

  4. #4
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    If you want to understand this you'll have to learn Swing. This looks like not too complicated a Swing application, so I recommend starting with picking up the basics.

  5. #5
    prof.deedee is offline Member
    Join Date
    Oct 2009
    Posts
    36
    Rep Power
    0

    Default

    Can u just explain the steps of the program...I mean I have basic in swing but I need more..so, can anyone help me??
    just describe what happen first then that step then.....

  6. #6
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    If you look at the main method, you can see where the program is going. It'll first call the constuctor, passing in a URL:

    public Browser(String initialPage){

    the initial page is the url string. Then it adds the URL string to a Vector (a special array) that stores the pages already viewed. Then it creates the window, a scrollbar inside of that window, a Label that shows the URL, buttons, and a toolbar. The buttons are registered to the listeners defined above the constructor. They listen for button actions and then execute based on what the button has done (such as if the button has been pressed).

  7. #7
    prof.deedee is offline Member
    Join Date
    Oct 2009
    Posts
    36
    Rep Power
    0

    Default

    OK AlbertoPL thank you so much for helping me but there are these questions:
    1-what u mean Vector (a special array)??
    2-how can I add textfield ???
    3-the user will enter the URL which he want in the textfield as String S and then I will do:
    initialPage=S;
    and when the user press Go button I will send that page to that constructor.. but how can I get the text from the textfield and in witch part of the program should I write this?

    I know how to do Go button but where I have to write it function???

  8. #8
    prof.deedee is offline Member
    Join Date
    Oct 2009
    Posts
    36
    Rep Power
    0

    Default

    OK I know now how to make jTextField the problem now is when the user enter the URL in that textfield and he press Go button where should I write :
    initialPage=userURL;
    and then calling the constructor
    I don't think it will be in the main because that happen only when the user press on GO button ??

  9. #9
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    You should be writing initialPage = url in the listener that the Go button is registered to. There is actually no such listener in the code, you will have to write that yourself.

    class backButtonListener implements ActionListener {

    }

    That handles the back button, you will want to create your go button listener in a similar way as the backButtonListener class.

    backButton.addActionListener(new backButtonListener(jep, backButton, history, label));

    That line registers the back button to the backButtonListener class, you will have to do a similar thing with the go button.

Similar Threads

  1. help to understand the ? mark in this code
    By carolain79@hotmail.com in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 06:57 AM
  2. Trying to understand this code
    By new2java2009 in forum New To Java
    Replies: 2
    Last Post: 09-09-2009, 07:18 PM
  3. cant understand what this means
    By hasysf in forum New To Java
    Replies: 19
    Last Post: 09-05-2009, 04:27 PM
  4. Help me understand this method
    By VinceGuad in forum New To Java
    Replies: 4
    Last Post: 04-11-2009, 01:00 AM
  5. I don´t understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-22-2009, 11:22 PM

Posting Permissions

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