Java Reference
In-Depth Information
public ShowDocument(AppletContext c, JTextField t)
{
context = c;
textField = t;
}
public void actionPerformed(ActionEvent a)
{
context.showStatus(“Showing document”);
URL url = null;
try
{
url = new URL(textField.getText());
}catch(MalformedURLException e)
{
e.printStackTrace();
context.showStatus(“Unable to display URL”);
return;
}
String label = a.getActionCommand();
if(label.equals(“Show”))
{
context.showDocument(url);
}
else if(label.equals(“Open in new window”))
{
context.showDocument(url, “_blank”);
}
}
}
Figure 14.15 shows the ContextDemo displayed in a Web browser. Let me
make a few comments about this applet:
The AppletContext for this applet is saved as a field in the class and ini-
tialized in the init() method. This is standard procedure with applets.
■■
The start() method attempts to display a message on the status bar of the
browser's window. I noticed that each time I ran the applet, the browser
would override the status bar after this, so I never saw the message.
■■
When either button is clicked, the actionPerformed() method in the
ShowDocument class is invoked. Setting the status bar to Showing
document worked every time.
■■
The java.net.URL constructor throws a MalformedURLException if the
String passed in is not in the proper URL format.
■■
Clicking the button labeled Show replaces the current page (con-
textdemo.html) with the URL entered in the text field.
■■
Clicking the other button shows the entered URL in a new browser
window.
■■
Search WWH ::




Custom Search