Thursday, June 25, 2009

Resizing Applets to Fit a Portlet

If the user resizes a window, or if he/she has a big enough screen, then the graph applet that does most of the monitoring won't look very good. To make it resizeable automatically requires some Javascript:

function resizeApplets()
{
    var i = 0;
    var applets = document.getElementsByTagName("applet");
    for ( ;i!=applets.length;i++ )
    {
        var width = applets[i].parentNode.offsetWidth;
        var height = applets[i].getHeight();
            applets[i].setSize( width, height );
        applets[i].width = width;
        applets[i].height = height;
    }
}
In this routine I look for all the applets on the page, and get the height and width of their immediate parents. Then I set the applet width and height to those values. Note also that I have to set the width and height in the HTML independently, so that the browser will know that the Applet is actually of a different size to what it was originally. (An Applet inherits all the public methods of the Java Applet class by the way). The disadvantage of this approach is that the height won't get changed, but perhaps we could feed in a new height from a form in the portlet. Hmm. I'll think about it. What we do with this is call it using the onresize and onload attributes for the body element. Then it gets called at the right times.

No comments:

Post a Comment