With applets, sometimes the browser acts as the applet’s frame. This prevents the user from mistaking the browser window for a separate application and closing it. With this approach, the applet does not resize itself as the user resizes the browser?but there are ways to do this.
The problem is because of the hard-coded applet dimensions the
We’ll use JavaScript to handle the browser’s onResize and onLoad events and pass on new dimensions to the applet. The applet can then override the setSize() method to revalidate its component layout, and thus let its layout manager adjust itself to the new size. The HTML looks like this:
Resizable Applet Demo
Internet Explorer and Netscape Navigator have somewhat different implementations of JavaScript. The onResize and onLoad parameters in the
tag specify the resize event handlers for Internet Explorer, while window.onResize =resize; and window.onLoad = resize; do so for Netscape Navigator. Both methods must be included in order to support the two major browsers. Whenever the browser frame is loaded or resized, the applet’s resize() method is invoked.Navigator and IE also have different methods of accessing the browser window’s dimensions. In Navigator, the window object is referenced; in IE, the document’s body object is used. These objects also return slightly different values for the window dimensions. Thus, it is necessary to determine the browser in which the applet is running before sending the correct window dimensions (see “Which Browser is Running Me?”). Netscape does not include the width of the scrollbars, and does not let you access this length, so 15 pixels are used as an offset in the netscapeScrollWidth variable.
The
public void setSize(int width, int height) { super.setSize(width,height); validate(); }
This approach has been tested on version 4.x of IE and Netscape for Microsoft Windows. Unix browsers are a little behind in their implementation of JavaScript.