HTMLWriterpublic class HTMLWriter extends Writer An output stream that sends HTML text to a newly created web browser window.
It relies on the netscape.javascript.JSObject class to send JavaScript
commands to the Web browser, and only works for applets running in
the Netscape Navigator Web browser. |
Fields Summary |
---|
JSObject | main_window | JSObject | window | JSObject | document | static int | window_num |
Constructors Summary |
---|
public HTMLWriter(Applet applet, int width, int height)When you create a new HTMLWriter, it pops up a new, blank, Web browser
window to display the output in. You must specify the applet
(this specifies the main browser window) and the desired size
for the new window. // used to give each new window a unique name
// Verify that we can find the JSObject class we need. Warn if not.
try { Class c = Class.forName("netscape.javascript.JSObject"); }
catch (ClassNotFoundException e) {
throw new NoClassDefFoundError("HTMLWriter requires " +
"Netscape Navigator 4.0 or higher");
}
// Get a reference to the main browser window from the applet.
main_window = JSObject.getWindow(applet);
// Create a new window to display output in.
window = (JSObject)
main_window.eval("self.open(''," +
"'HTMLWriter" + window_num++ + "'," +
"'menubar,status,resizable,scrollbars," +
"width=" + width + ",height=" + height + "')");
// Obtain the Document object of this new window, and open it.
document = (JSObject) window.getMember("document");
document.call("open", null);
|
Methods Summary |
---|
public void | close()When the stream is closed, close the JavaScript Document object
(But don't close the window yet.) document.call("close", null); document = null;
| public void | closeWindow()If the browser window is still open, close it.
This method is unique to HTMLWriter.
if (document != null) close();
if (!((Boolean)window.getMember("closed")).booleanValue())
window.call("close", null);
window = null;
| public void | finalize()A finalizer method to close the window in case we forget. closeWindow();
| public void | flush()There is no general way to force JavaScript to flush all pending output,
so this method does nothing. To flush, output a tag or some other
HTML tag that forces a line break in the output.
| public void | write(char[] buf, int offset, int length)This is the write() method required for all Writer subclasses.
Writer defines all its other write() methods in terms of this one.
// If no window or document, do nothing. This occurs if the stream
// has been closed, or if the code is not running in Navigator.
if ((window == null) || (document == null)) return;
// If the window has been closed by the user, do nothing
if (((Boolean)window.getMember("closed")).booleanValue()) return;
// Otherwise, create a string from the specified bytes
String s = new String(buf, offset, length);
// And pass it to the JS document.write() method to output the HTML
document.call("write", new String[] { s });
|
|