Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Feb 18, 2000

Provide Default Values for Applet Parameters

You can customize your applets by providing parameters at runtime in <param>. But it is a good practice to always provide default values, when specific values for parameters are not provided.

You can do this by checking for null values and/or proper type checking (if you expect numeric or date values). Here is the code:
 
public class MyApplet extend Applet {

         public void  init(){

          // Get the Parameter
	  String  frameTitle = getParameter("FRAME_TITLE");
	  // Check for Null
	   if (frameTitle  == null) {
	       // Provide Default value
	        frameTitle = "Frame to Test Parameters";
	   }

	  String frameWidthString = getParameter("FRAME_WIDTH");
	  int   frameWidthInt = 0;

	  try{
	        frameWidthInt =  Integer.parseInt(frameWidthString);
	  } catch(Exception ex){
	       // Catch Exception if frameWidthString is null or invalid value
               // Default value
	       frameWidthInt = 100;
	  }

	  String frameHeightString = getParameter("FRAME_WIDTH");
	  int   frameHeightInt = 0;
	  try{
	        frameHeightInt =  Integer.parseInt( frameHeightString );
	  } catch(Exception ex){
	       // Provide Default Value
	       frameHeightString = 100;
	  }

	 Frame f = new Frame(frameTitle);
	 f.setSize(frameWidthInt,frameHeightString);
	 f.show();
         }
}
Nilesh Shah
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap