How to Resize Applets Dynamically within Browser Frames
The problem I was trying to solve was to resize applets when the browser changes size,
so as to always fill the total displayed area of the browser.
Googling I found an article from javaworld - Resize applets within browser frames.
I soon found out that the solution doesn't work on either Internet Explorer 6 or Netscape 7.1.
The key problem is that applet.setSize() method doesn't actually do anything!
So I did a bit of digging around and found a simpler solution.
The code shown below creates an applet with the same size as the browser window.
Whenever the browser changes size the applet is resized dynamically. This is
achieved through the resize() method defined in Javascript. This method is
invoked onLoad and onResize. The code works for the recent versions of
Internet Explorer & Netscape browsers.
Resizable Applet Demo
The two lines:
document.myApplet.width = width;
document.myApplet.height = height;
are enough to change the width and height of the applet.
Remember to name the applet and refer to it by name.
Footnote: EU has proposed a simpler solution to the original problem which is to set the width and height to 100% in the applet tag. And that works for the problem I proposed. Thanks for the solution EU!
There are a few additional value to the original proposed solution over the new solution which are:
- Finer control over the size of the applet, so for example you can say that the width leaves 20 pixels on both sides, but the height doesn't etc.
- Ability to control the size of the applet from the applet itself. This is a big plus as in one project I am working we have a need to re-size the applet from within the applet. Now we can do it by calling a Javascript method from the applet.
Filed under How To, Java Software |
10 Comments |
Email this Article

















January 24th, 2004 at 11:56 am
What about use “100%” for applet width and height?
August 29th, 2004 at 9:31 pm
[...] x 0.9.1 resize() showDocument() Internet Explorer 6.0 resize() I had posted a solution for the applet resize problem in an earlier blog entry.
Comments (0) [...]
March 11th, 2005 at 4:23 pm
[...] an it has been allocated, then how to go about it? Last time when faced with this problem I solved it with a nifty piece of Javascript method. However I soon realized there were few issues with t [...]
March 11th, 2005 at 4:36 pm
[...] Fox 0.9.1 resize() showStatus() Internet Explorer 6.0 resize() I had posted a solution for the applet resize problem in an earlier blog entry. This entry was [...]
June 23rd, 2005 at 6:07 am
You little beauty - this had been driving me mad. Your example has lots of unrequired backslashes for some reason though
June 23rd, 2005 at 6:26 am
@Paul
The backslashes are an unfortunate artifact of the CMS software I am using.
I have removed them now.
August 12th, 2005 at 1:35 pm
How about if you have frameset and frame is resized, applet does not get resized.
applet gets resized only after window reload.
sequence:
have a frame set with Jtree in one frame
applet in another frame, I want to resize
the applet when frame is resized.
the window reload, resize it works, but not frame resize by dragging the frame boundary between two frames.
June 9th, 2008 at 9:00 am
The applet is not working anymore
October 8th, 2008 at 4:00 am
this solution does not work on
vista + firefox 3.0.3 || iexploder 7