FileDocCategorySizeDatePackage
Chapter1.javaAPI DocExample5812Thu Oct 04 11:59:12 BST 2001None

Ch1Sol1ab

public class Ch1Sol1ab extends JFrame implements ActionListener, HyperlinkListener

Fields Summary
private JEditorPane
html
private URL
url
private JTextField
location
private JLabel
bottomLine
Constructors Summary
public Ch1Sol1ab(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 );

/**/	bottomLine = new JLabel( "" );
/**/  this.getContentPane().add( bottomLine, 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)

	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)
/**/		{	loadURL( e.getURL().toString() );
		}
	
private voidloadURL(java.lang.String site)

	try
		{	url = new URL( site );
		}
		catch( MalformedURLException e )
/**/	{	bottomLine.setText( "Invalid URL specified" );
/**/	   return;
		}
		try
		{	html.setPage( url ); // load page
		}
		catch( IOException e )
/**/	{	bottomLine.setText( "Can't load url: " + url );
/**/		return;
		}
/**/	bottomLine.setText( "" );
/**/  // update text field (moved)
/**/  location.setText( site );	
	
public static voidmain(java.lang.String[] args)

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