private List history = new ArrayList();
private int historyIndex;
// WebBrowserPane constructor
public WebBrowserPane()
{
// disable editing to enable hyperlinks
setEditable( false );
}
// display given URL and add it to history
public void goToURL( URL url )
{
displayPage( url );
history.add( url );
historyIndex = history.size() - 1;
}
// display next history URL in editorPane
public URL forward()
{
historyIndex++;
// do not go past end of history
if ( historyIndex >= history.size() )
historyIndex = history.size() - 1;
// WebToolBar.java
// WebToolBar is a JToolBar subclass that contains components
// for navigating a WebBrowserPane. WebToolBar includes back
// and forward buttons and a text field for entering URLs.
// create JButton for navigating to next history URL
forwardButton = new JButton( new ImageIcon(
getClass().getResource( "images/forward.gif" ) ) );
forwardButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
// navigate to next URL
URL url = webBrowserPane.forward();
// display new URL in urlTextField
urlTextField.setText( url.toString() );
}
}
);
// add JButtons and JTextField to WebToolBar
add( backButton );
add( forwardButton );
add( urlTextField );
} // end WebToolBar constructor
// listen for HyperlinkEvents in WebBrowserPane
public void hyperlinkUpdate( HyperlinkEvent event )
{
// if hyperlink was activated, go to hyperlink's URL
if ( event.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED ) {
// get URL from HyperlinkEvent
URL url = event.getURL();
// navigate to URL and display URL in urlTextField
webBrowserPane.goToURL( url );
urlTextField.setText( url.toString() );
}
}
}