setSize( 550, 600 );
setTitle( "Chapter Two Solutions" );
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e )
{ System.exit( 0 );
}
});
// Build more interesting interface
/**/ location = new JComboBox();
/**/ location.setEditable( true );
/**/ location.getEditor().setItem( site );
JPanel topLine = new JPanel();
topLine.setLayout( new FlowLayout( FlowLayout.LEFT ) );
topLine.add( back );
topLine.add( forward );
topLine.add( new JLabel( "URL:" ) );
topLine.add( location );
// for the JFrame:
this.getContentPane().setLayout( new BorderLayout() );
this.getContentPane().add( topLine, BorderLayout.NORTH );
this.getContentPane().add( statusLine, BorderLayout.SOUTH );
if( !site.equals( "NoURL" ) )
{ if( loadURL( site ) )
{ try
{ historyList.add( new URL( site ) );
}
catch( Exception ex ) {} // it must work as loadURL returned true.
location.addItem( 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
setVisible( true );
/**/ // Event handlers - new code but much has simply been moved here
// First pick up <return> pressed in editable section of the
// JComboBox. Need to add the new item to the JComboBox list.
location.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e )
{ String newSite = location.getEditor().getItem().toString();
if( loadURL( newSite ) )
{ try
{ historyList.add( new URL( newSite ) );
}
catch( Exception ex ) {} // it must work as loadURL returned true.
int index = -1;
for( int item = 0; item < location.getItemCount(); item++ )
{ if( location.getItemAt( item ).equals( newSite ) )
{ index = item;
}
}
if( index == -1 ) // new item, not in list, so put at front
{ location.insertItemAt( newSite, 0 );
}
}
}
});
// This one is needed to capture an item picked from the list
location.addItemListener( new ItemListener() {
public void itemStateChanged( ItemEvent e )
{ if( e.getStateChange() == ItemEvent.SELECTED )
{ boolean b = loadURL( e.getItem().toString() ); // must work!
}
}
});
// history list buttons:
back.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e )
{ historyList.back();
loadURL( historyList.getCurrent().toString() );
}
});
forward.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e )
{ historyList.forward();
loadURL( historyList.getCurrent().toString() );
}
});
/**/ // hyperlinkListener moved to here
html.addHyperlinkListener( new HyperlinkListener() {
public void hyperlinkUpdate( HyperlinkEvent e )
{ if( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED )
{ if( loadURL( e.getURL().toString() ) )
{ historyList.add( e.getURL() );
}
}
if( e.getEventType() == HyperlinkEvent.EventType.ENTERED )
{ statusLine.setText( "" + e.getURL() );
}
if( e.getEventType() == HyperlinkEvent.EventType.EXITED )
{ statusLine.setText( "" );
}
}
});
/**/ // end of event handlers