Extended Applet Example Poetry In Browser


Using Applet Parameters

Applets can utilize parameter strings passed to it from its HTML page via the <param> tag. The <param> tag takes the following form:

 < param   name ="parameter_name"  value ="parameter_value" >

The <param> tag has two attributes: name and value. Notice that both the name and value attributes are strings. To use a parameter in an applet you get the parameter value by calling the Applet.getParameter(String name) method using the parameter’s name as an argument to the method call.

Examples 21.7 and 21.8 implement a short applet named ParameterApplet that utilizes two parameters to print a short poem by Ogden Nash in a JTextArea.

Example 21.7: ParameterApplet.java

image from book
 1     import javax.swing.*; 2     import java.util.*; 3 4     public class ParameterApplet extends JApplet { 5 6         private JTextArea _textarea = null; 7         private JScrollPane _scrollpane = null; 8 9         public void init(){ 10          _textarea = new JTextArea(10, 20); 11          _scrollpane = new JScrollPane(_textarea); 12          this.getContentPane().add(_scrollpane); 13       } 14  15       public void start(){ 16         _textarea.append(this.getParameter("WelcomeMessage") + '\n'); 17         _textarea.append("\n"); 18         StringTokenizer st = new StringTokenizer(this.getParameter("Poem"), "-"); 19         while(st.hasMoreTokens()){ 20           _textarea.append(st.nextToken() + '\n'); 21         } 22      } 23    } // end ParameterApplet class definition
image from book

Example 21.8: parameterapplet.html

image from book
 1        <html> 2          <applet code="ParameterApplet.class" height=300 width=300> 3          <param name="WelcomeMessage" value="A nice poem by Ogden Nash..."> 4          <param name="Poem" value="Always Marry An April Girl- -Praise the spells and bless the charms,-I found April in my arms.-April golden, April cloudy,-Gracious, cruel, tender, rowdy;-April soft in flowered languor,-April cold with sudden anger,-Ever changing, ever true-I love April, I love you." > 5           </applet> 6         </html>
image from book

Referring first to example 21.7 — the ParameterApplet class declares a JTextArea and a JScrollPane and initializes these components in its init() method. On line 16 of the start() method it reads the first parameter named WelcomeMessage and displays its string value in the JTextArea. The next line simply appends a new line to the JTextArea. A StringTokenizer object is created on line 18 using the string value of the second parameter named Poem. The while loop on line 19 loops through all the StringTokenizer’s tokens and appends them to the JTextArea.

Referring to example 21.8 — the two parameters used in ParameterApplet are declared using the <param> tags on lines 3 and 4. Line 4 is one long line and is shown here wrapped. Notice how the StringTokenizer splits the long Poem parameter string into tokens based on the “-” string. Figure 21-10 shows the results of running this applet in a web browser.

image from book
Figure 21-10: Results of Running ParameterApplet

Since all parameter values are strings, you may have to utilize the wrapper classes like Integer, Float, etc., to convert string values to primitive type values if you require these as parameters in your applet.

Quick Review

Use <param> tags to embed applet parameters in HTML pages. Use the Applet class’s getParameter() method to fetch parameter values and use them in your program. Use wrapper classes as required to convert parameter value strings to primitive type values.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net