«^»
4.9. Reworking an application as an applet: GetDateApplet

Many of the programs that you have already produced can easily be rewritten as Java applets. Often this can be done by putting the code of the main method into an applet's init method. For example, we could take the statements of the main method of the GetDateProg program (given earlier in Stage F) and put them into an init method of a GetDateApplet class. The resulting code is shown here:

0341: //                                                     // GetDateApplet.java
0342: // An applet containing the button to get the date and time.
0343: // Barry Cornelius, 22nd November 1999
0344: import java.awt.    BorderLayout;
0345: import java.awt.    Container;
0346: import javax.swing. JApplet;
0347: import javax.swing. JButton;
0348: import javax.swing. JFrame;
0349: import javax.swing. JTextField;
0350: public class GetDateApplet extends JApplet
0351: {
0352:    public void init()
0353:    {
0354:       final JFrame tJFrame = new JFrame("GetDateApplet");
0355:       final JTextField tJTextField = new JTextField("hello", 35);
0356:       final JButton tJButton = new JButton("Get Date");
0357:       final JButtonListener tJButtonListener =
0358:                                   new JButtonListener(tJTextField);
0359:       tJButton.addActionListener(tJButtonListener);
0360:       final Container tContentPane = tJFrame.getContentPane();
0361:       tContentPane.add(tJTextField, BorderLayout.NORTH);
0362:       tContentPane.add(tJButton, BorderLayout.SOUTH);
0363:       tJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
0364:       tJFrame.pack();
0365:       tJFrame.setVisible(true);
0366:    }
0367: }

In producing this class, the two statements of GetDateProg that establish a window listener:

final ExitOnWindowClosing tExitOnWindowClosing = new ExitOnWindowClosing();
tJFrame.addWindowListener(tExitOnWindowClosing);
have been omitted. This is because the ExitOnWindowClosing class has a windowClosing method that calls System.exit. For the reasons explained in the previous section, it is not appropriate for an applet to have this code.

When an applet is executed, if it creates any new windows then these will appear with a yellow warning banner displaying the text Warning: Applet Window. This text warns the user of the WWW browser that the window being displayed has not been produced by a trusted applet. The WWW page at http://java.sun.com/products/plugin/plugin.faq.html says that the yellow warning banner is an important security feature. It cannot be disabled by untrusted applets. If you use a signed applet, where the signing key is trusted by the end user, then the warning banner will not be shown.

Here is a link to a WWW page containing HTML instructions to run this applet: http://www.dur.ac.uk/barry.cornelius/papers/advanced+/code2/Applets/GetDateApplet/GetDateApplet.html.

Instead of getting the applet to produce a new window (tJFrame), we can use the pane of the applet itself. In the following code, this is used to refer to the GetDateAppletPane object (which is derived from JApplet):

0368: //                                                 // GetDateAppletPane.java
0369: // An applet containing the button to get the date and time.
0370: // Barry Cornelius, 22nd November 1999
0371: import java.awt.    BorderLayout;
0372: import java.awt.    Container;
0373: import javax.swing. JApplet;
0374: import javax.swing. JButton;
0375: import javax.swing. JTextField;
0376: public class GetDateAppletPane extends JApplet
0377: {
0378:    public void init()
0379:    {
0380:       final JTextField tJTextField = new JTextField("hello", 35);
0381:       final JButton tJButton = new JButton("Get Date");
0382:       final JButtonListener tJButtonListener =
0383:                                   new JButtonListener(tJTextField);
0384:       tJButton.addActionListener(tJButtonListener);
0385:       final Container tContentPane = this.getContentPane();
0386:       tContentPane.add(tJTextField, BorderLayout.NORTH);
0387:       tContentPane.add(tJButton, BorderLayout.SOUTH);
0388:    }
0389: }

Here is a link to a WWW page containing HTML instructions to run this applet: http://www.dur.ac.uk/barry.cornelius/papers/advanced+/code2/Applets/GetDateAppletPane/GetDateAppletPane.html.