FileDocCategorySizeDatePackage
Browser4.javaAPI DocExample5640Mon Oct 16 19:44:08 BST 2000None

Browser4

public class Browser4 extends JFrame implements ActionListener, HyperlinkListener

Fields Summary
private JEditorPane
html
private JTextField
location
private JLabel
statusLine
private JButton
back
private JButton
forward
private HistoryList
historyList
Constructors Summary
public Browser4(String site)


	    
		setSize( 550, 450 );
		setTitle( "Simple Web Browser #4" );
		addWindowListener( new WindowAdapter() {
			public void windowClosing( WindowEvent e )
			{	System.exit( 0 );
			}
		});

		// Build more interesting interface
		location = new JTextField( site, 25 );  // 25 cols
		JPanel topLine = new JPanel();
		topLine.setLayout( new FlowLayout( FlowLayout.LEFT ) );
      topLine.add( back );
      topLine.add( forward );
		topLine.add( new JLabel( "URL:" ) );
		topLine.add( location );
		location.addActionListener( this );
      back.addActionListener( this );
      forward.addActionListener( this );
		
		// for the JFrame:
		this.getContentPane().setLayout( new BorderLayout() );
		this.getContentPane().add( topLine, BorderLayout.NORTH );
		this.getContentPane().add( statusLine, BorderLayout.SOUTH );

		if( !site.equals( "NoURL" ) )
		{	loadURL( site );
		}
		
		JScrollPane scroller = new JScrollPane();
		JViewport viewport = scroller.getViewport();
		viewport.add( html );
		
		this.getContentPane().add( scroller, BorderLayout.CENTER );
		html.setEditable( false ); // to allow links to be activated
		html.addHyperlinkListener( this );
		
		setVisible( true );		
	
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

	// need to check which object called us
      if( e.getSource() == location )
      {  String site = location.getText();
		   loadURL( site );
      }
      if( e.getSource() == back )
      {	historyList.back();
      	try
			{
				html.setPage( historyList.getCurrent() );
			}
			catch( IOException ioe )
			{	System.out.println( "Can't load url: " + historyList.getCurrent() );
				System.exit( 0 );
			}
			location.setText( historyList.getCurrent().toString() );
     	}
      if( e.getSource() == forward )
      {	historyList.forward();
      	try
			{	html.setPage( historyList.getCurrent() ); 
			}
			catch( IOException ioe )
			{	System.out.println( "Can't load url: " + historyList.getCurrent() );
				System.exit( 0 );
			}
			location.setText( historyList.getCurrent().toString() );
      }
	
public voidhyperlinkUpdate(javax.swing.event.HyperlinkEvent e)

	if( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED )
		{	loadURL( e.getURL().toString() );
			location.setText( "" + e.getURL() );
		}
		if( e.getEventType() == HyperlinkEvent.EventType.ENTERED )
		{	statusLine.setText( "" + e.getURL() );
		}
		if( e.getEventType() == HyperlinkEvent.EventType.EXITED )
		{	statusLine.setText( "" );
		}
	
private voidloadURL(java.lang.String site)

	URL url = null;
	   try
		{	url = new URL( site );
		}
		catch( MalformedURLException e )
		{	System.out.println( "Invalid URL specified" );
			System.exit( 0 );
		}
		try
		{	html.setPage( url ); // load page
		}
		catch( IOException e )
		{	System.out.println( "Can't load url: " + url );
			System.exit( 0 );
		}
		// must be OK so add to history
		historyList.add( url );
	
public static voidmain(java.lang.String[] args)

	if( args.length == 0 )
		{	new Browser4( "NoURL" );
		}
		else
		{	new Browser4( args[0] );
		}