Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Tip 80: Resize applets within browser frames - JavaWorld

Tip 80: Resize applets within browser frames - JavaWorld

Tutorial Details:

Tip 80: Resize applets within browser frames
Tip 80: Resize applets within browser frames
By: By Francis Lu
Break the rules with a little bit of JavaScript
hen an applet is embedded within a Web browser, the browser becomes the applet's frame. However, when the user resizes the browser frame, the applet does not adjust its size to fit within the new dimensions. This is because applets have hard-coded dimensions in their tags. Therefore, despite the fact that your applet may be using layout managers, it won't adjust its components accordingly. A little JavaScript easily remedies this problem.
A complete example
This approach uses JavaScript to handle the browser's onResize and onLoad events and forward new dimensions to the applet. The applet overrides 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

topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">

HEIGHT=1200>/APPLET>


Internet Explorer and Netscape Navigator implement JavaScript in slightly different ways. 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 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. Netscape does not include the width of the scrollbars, and does not let you access this length, so 15 pixels is used as an offset in the netscapeScrollWidth variable.
The tag still specifies the applet's Width and Height parameters, but these dimensions now specify the applet's maximum bounds. The window's maximum width and height, w_maxWidth and w_maxHeight , are checked in the resize() method to ensure that the applet does not go beyond these bounds. The browser still takes up room for this dimension; thus, the scrollbars will not show all the information in the window. Signed JavaScript in Netscape has the ability to hide the scrollbars. The HTML page is also scrolled to the top left corner whenever it is resized to ensure that the applet is fully visible.
The Java code to support the dynamic resizing is trivial. The applet's setSize() method must be overridden to call the validate() method.
public void setSize(int width, int height)
{
super.setSize(width,height);
validate();
}
The example applet has an increment button and a label to illustrate the fact that the applet does not reinitialize when a resize event is processed. The full source of the applet is available in the Resources section.
Known limitations
Currently, JavaScript-to-Java communication is available in Windows versions of Netscape Navigator 4.0 and up and Internet Explorer 4.0 and up, and in Solaris and Linux versions of Navigator 4.0 and up. Support of this feature is being adopted in more Unix browsers, as the LiveConnect-SDK for Unix is available. It is not supported on the Macintosh platform.
Alternative approaches
Another approach to resizing the applet is to create the page dynamically each time the window is resized using the document.write() method. When the user resizes the window, the HTML page is reloaded, and the document is rewritten with a new tag that specifies the corresponding width and height boundaries. This approach is a little clumsy, because browsers can react prematurely when asked to reload the current document while resizing. They sometimes attempt to reload before the user is done resizing, thus providing an inaccurate size to the tag after resizing is complete.
To remove the gray/white space represented by the page's unused applet region, you could create a base applet that contains the real applet. This base applet's sole reasons for existence are, first, to react to the setSize() method and delegate the event to the second applet, which it contains; and second, to paint a background color or image that blends well with the HTML background. The base applet could also become reusable if the background color is passed as a from the HTML. This background color would be the same color used as the 's background color.
A perfect match
JavaScript is usually thought of as an HTML designer's tool, rather than part of a Java developer's repertoire. However, when JavaScript and Java are combined, the two complement each other very well. This approach uses JavaScript-to-Java communication to solve the problem of forwarding the resize events to an embedded applet. As shown by this article's example, the combination of JavaScript and Java has a great deal of potential. And hopefully, as Java developers, we will learn more ways to utilize it.
This page formated for crawlers and browsers that don't support scripts and tables.
Home
EZone


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Tip 80: Resize applets within browser frames - JavaWorld

View Tutorial:
Tip 80: Resize applets within browser frames - JavaWorld

Related Tutorials:

Saving and retrieving objects with Java
Saving and retrieving objects with Java
 
Reloading Applets
Reloading Applets
 
Applet to Applet Communication
Applet to Applet Communication
 
How to write a Java Card applet: A developer's guide
How to write a Java Card applet: A developer's guide
 
Create a scrollable virtual desktop in Swing
Create a scrollable virtual desktop in Swing
 
Java Tip 132: The taming of the thread
Java Tip 132: The taming of the thread
 
Update distributed applications
Update distributed applications
 
Enhance your J2EE presentation layer
Enhance your J2EE presentation layer
 
Protect Web application control flow
Protect Web application control flow
 
Java Tip 143: Utilize the Database Schema Browser
Java Tip 143: Utilize the Database Schema Browser
 
Hermes JMS Browser
Hermes JMS Browser Hermes is a Swing application that allows you to interact with JMS providers.
 
End-to-end internationalization of Web applications
End-to-end internationalization of Web applications Going beyond the JDK A typical Web application workflow involves a user loading one of your Webpages into her browser, filling out HTML form parameters, and submitting data back to the server. The ser
 
Internet & Intranets: Java Servlets
What are servlets? "Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to upd
 
Object-Oriented Language: Java / APIs (Classes & Libraries)
The Java Platform APIs are a set of essential interfaces that developers need to build their Java applications and applets. All Java Platfrom APIs are open and extensible, and are created by JavaSoft and industry-wide specialists in each target technology
 
Introduction to Servlets, JSP, and Servlet Engines
Servlets are the Java Technologies' answer to CGI programming. They are programs which run on the server side and generate dynamic content. Why would one prefer to use Servlets over traditional CGI programming?
 
Unclog the server bottleneck with active containers.
In server-side control architectures such as Java ServerFaces (JSF) or Struts, a majority of the control events must be handled on the server side to update the state of the control. For every user event, the entire page data is sent back to the server...
 
Understanding Network Class Loaders Class loaders
One of the cornerstones of Java dynamics, determine when and how classes can be added to a running Java environment.
 
Understanding MIDP System Threads
Describes the multi-threaded aspects of the J2ME application environment. Understanding the interactions between systems threads, user-interface and application threads will help in avoiding MIDlet deadlock.
 
Getting Started With Bluetooth
JSR-82 brings Bluetooth API's to the J2ME environment. Read the tech tip and begin experimenting with Bluetooth today using the Wireless Toolkit 2.2 Beta.
 
Enhance your end-user MDI experience with JExplose
Enhance your end-user MDI experience with *JExplose* ! Extremely simple to integrate with your existing Swing MDI products.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.