Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

understanding the code

setpreferredsize

  • Please log in to reply
3 replies to this topic

#1 prof.deedee

prof.deedee

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 50 posts

Posted 10 November 2009 - 05:41 AM

Hi
please help me ... I found this code in the internet and I think it will help me but I can't understand it very well...can anyone help me please??:rules:

This code is to make simple web browser for u ...:w00t:


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("http://www.cse.sc.edu");

		if (args.length > 0) initialPage = args[0];

		Browser b = new Browser(initialPage);
	}
	
}

  • 0

#2 prof.deedee

prof.deedee

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 50 posts

Posted 10 November 2009 - 12:46 PM

pleeeeeeaaaaaase any help
  • 0

#3 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 10 November 2009 - 01:48 PM

Is there any particular part of the code that is confusing you?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 prof.deedee

prof.deedee

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 50 posts

Posted 10 November 2009 - 04:33 PM

yes what setPage method do??
  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download