Java Reference
In-Depth Information
Example 10•21: WebBrowser.java (continued)
* history list.
**/
public void displayPage(URL url) {
if (visit(url)) { // go to the specified url, and if we succeed:
history.add(url); // Add the url to the history list
int numentries = history.size();
if (numentries > MAX_HISTORY+10) { // Trim history when too large
history = history.subList(numentries-MAX_HISTORY, numentries);
numentries = MAX_HISTORY;
}
currentHistoryPage = numentries-1; // Set current history page
// If we can go back, then enable the Back action
if (currentHistoryPage > 0) backAction.setEnabled(true);
}
}
/** Like displayPage(URL), but takes a string instead */
public void displayPage(String href) {
try {
displayPage(new URL(href));
}
catch (MalformedURLException ex) {
messageLine.setText("Bad URL: " + href);
}
}
/** Allow the user to choose a local file, and display it */
public void openPage() {
// Lazy creation: don't create the JFileChooser until it is needed
if (fileChooser == null) {
fileChooser = new JFileChooser();
// This javax.swing.filechooser.FileFilter displays only HTML files
FileFilter filter = new FileFilter() {
public boolean accept(File f) {
String fn = f.getName();
if (fn.endsWith(".html") || fn.endsWith(".htm"))
return true;
else return false;
}
public String getDescription() { return "HTML Files"; }
};
fileChooser.setFileFilter(filter);
fileChooser.addChoosableFileFilter(filter);
}
// Ask the user to choose a file.
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
// If they didn't click "Cancel", then try to display the file.
File selectedFile = fileChooser.getSelectedFile();
String url = "file://" + selectedFile.getAbsolutePath();
displayPage(url);
}
}
/** Go back to the previously displayed page. */
public void back() {
if (currentHistoryPage > 0) // go back, if we can
Search WWH ::




Custom Search