FileDocCategorySizeDatePackage
Browser3.javaAPI DocExample3018Mon Oct 16 19:44:08 BST 2000None

Browser3

public class Browser3 extends JFrame implements ActionListener, HyperlinkListener

Fields Summary
private JEditorPane
html
private URL
url
private JTextField
location
Constructors Summary
public Browser3(String site)

	    
		setSize( 350, 450 );
		setTitle( "Scrolling Browser" );
		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( new JLabel( "URL:" ) );
		topLine.add( location );
		location.addActionListener( this );
		
		// for the JFrame:
		this.getContentPane().setLayout( new BorderLayout() );
		this.getContentPane().add( topLine, BorderLayout.NORTH );

		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)

	String site = location.getText();
		loadURL( site );
		// big assumption - it is legal and works!!
	
public voidhyperlinkUpdate(javax.swing.event.HyperlinkEvent e)

	if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
		{	try
			{  html.setPage( e.getURL() );
			}
			catch( IOException ioe )
			{	System.out.println( "Can't load url: " + url );
				System.exit( 0 );
			}
		}
		// update text field
		location.setText( "" + e.getURL() );
	
private voidloadURL(java.lang.String site)

	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 );
		}
	
public static voidmain(java.lang.String[] args)

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