[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Elegant applet resizing
JavaScript should be able to access properties of your applet from inside
the HTML of the page. You could have your applet set some preferred size
attributes after loading itself (based on the data size, as you indicate),
then write some JavaScript code in the HTML of the page that would retrieve
these preferred size attributes and do a location.href =
'thispage.htm?Height=' + retrievedHeight + '&Width=' + retrievedWidth. You
can then dynamically set the height and width attributes of the <applet> (or
<object>, <applet> has been deprecated) tag using these URL variables.
Some possible sticking points include;
1) You'll need defaults for the height & width attributes of the <applet> or
<object> tag for the initial case when the page is loading w/o being
reloaded by your JavaScript which specifies the URL variables used to get
these values dynamically.
2) You'll need to verify that in-page JavaScript can see attributes set by
the applet programmatically. I know the JavaScript can see attributes that
were specified in <param> tags inside the <applet> or <object> tag, but I'm
not sure about new attributes your applet sets internally. I am pretty sure
it can be done, but you'll want to verify, as the whole approach depends on
this ability. I have not written too many applets, so I'll a little rusty
on the details ;-)
3) The timing of when your JavaScript routine that reads these attributes
and reloads the page may be a bit tricky. You could initially try to use
the onLoad HTML DOM event of your <object> or <applet> tag to run this
routine. My suspicion however, is that the onLoad event will fire as soon
as
either (a) the <applet> or <object> tag is parsed, or (b) the codebase .jar
or .class files are downloaded, either of which may be too soon for you.
However, there's the possibility that the onLoad JavaScript event won't fire
until after your applet has been properly initialized, which would be OK, so
you should at least try using the onLoad event to simply invoke your
routine. If that does not work, you'll need to introduce a delay, I'd
recommend putting some extra code in your routine and still calling it in
the onLoad event. In JavaScript, you use the setTimeout("function_name",
number_of_milliseconds) routine to do that. It calls your function after
the specified number of milliseconds have elapsed. It may be hard to
predict exactly how many milliseconds are needed, as that will depend on
both the person's connections speed and machine speed. You may be better
off with a polling loop type thing. I found the following code in Danny
Goodman's "JavaScript Bible" (great JS book, by the way) that does what
you'd need:
var t;
function doAction() {
if(!document.myApplet.ready) {
t = setTimeout("doAction", 500);
} else {
clearTimeout(t);
// your code to use the applet here, in your case, get the sizes
and reload the page
}
}
Using this code, the "ready" attribute would be a flag for your applet,
probably initialized to false in a <param> tag, and then changed to true by
the applet when it's got the data and set the preferred size attributes, so
the JavaScript can tell that it's safe to run that code.
4) Very Important! To avoid getting stuck in an infinite loop where the
page reloads itself non-stop, your JavaScript routine should wrap all its
code in an if() statement that only executes if the URL variables for height
& width are *not* defined, thus indicating that the applet has not yet
resized itself.
This *may* work, but I would not say it's particularly elegant,
unfortunately ;-)
Hope this helps.
Jeff
----- Original Message -----
From: "John Wells" <jb@sourceillustrated.com>
To: <ajug-members@ajug.org>
Sent: Monday, June 09, 2003 10:55 PM
Subject: Elegant applet resizing
> I have an applet that's essentially used as a control in a web page. The
> applet is merely a JTable which accesses the database and pulls back a
> representation of a particular table and displays it to the user.
>
> I'd like to have the applet resize itself based on the size of the table
> contents (unless the size would cause it to grow larger than 800 width).
>
> Is there an elegant (read correct) way to do this? Afaik, the object tag
> requires height and width to be specified in the html tag *before* the
> applet is loaded, which would imply that the sizing occurs before my
> database queries execute.
>
> Any suggestions/links are greatly appreciated.
>
> John
>
>
>
>
>