Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-10-2009, 03:25 PM
Member
 
Join Date: Oct 2009
Posts: 20
Rep Power: 0
prof.deedee is on a distinguished road
Default understand the code
Hi
I found this code in the Internet and I think it will help me to create simple web browser but because I am new in Java I can't understand everything in it ....... can anyone help me???

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);
	}
	
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-10-2009, 03:28 PM
Senior Member
 
Join Date: Aug 2009
Posts: 1,760
Rep Power: 2
r035198x is on a distinguished road
Default
What exactly do you not understand? Everything?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-10-2009, 04:09 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 930
Rep Power: 2
JosAH is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-10-2009, 09:07 PM
Member
 
Join Date: Sep 2009
Posts: 22
Rep Power: 0
AlbertoPL is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-10-2009, 09:32 PM
Member
 
Join Date: Oct 2009
Posts: 20
Rep Power: 0
prof.deedee is on a distinguished road
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.....
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-10-2009, 09:59 PM
Member
 
Join Date: Sep 2009
Posts: 22
Rep Power: 0
AlbertoPL is on a distinguished road
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).
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-10-2009, 11:42 PM
Member
 
Join Date: Oct 2009
Posts: 20
Rep Power: 0
prof.deedee is on a distinguished road
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???
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-11-2009, 12:10 AM
Member
 
Join Date: Oct 2009
Posts: 20
Rep Power: 0
prof.deedee is on a distinguished road
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 ??
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-11-2009, 03:43 AM
Member
 
Join Date: Sep 2009
Posts: 22
Rep Power: 0
AlbertoPL is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help to understand the ? mark in this code carolain79@hotmail.com New To Java 2 10-13-2009 07:57 AM
Trying to understand this code new2java2009 New To Java 2 09-09-2009 08:18 PM
cant understand what this means hasysf New To Java 19 09-05-2009 05:27 PM
Help me understand this method VinceGuad New To Java 4 04-11-2009 02:00 AM
I don´t understand Manikyr New To Java 6 02-23-2009 12:22 AM


All times are GMT +2. The time now is 03:40 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org