This applet tests two custom components, MirrorLabel and MirrorLabel . It also tests the validate() method. The user can click a button to change the text on the components in the applet. Clicking another button will validate the applet. This will cause components that have been invalidated to be resized. The MirrorLabel is automatically invalidated when its text is changed. For the StopWatch and Buttons, it might be platform-dependent.

/*******************************************************
*     MYCPLUS Sample Code - https://www.mycplus.com     *
*                                                     *
*   This code is made available as a service to our   *
*      visitors and is provided strictly for the      *
*               purpose of illustration.              *
*                                                     *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComponentTest extends JApplet {

   MirrorLabel greet;
   StopWatch timer;
   JButton changeText;

   public void init() {

       getContentPane().setLayout(new FlowLayout());

       greet = new MirrorLabel("PLEASE LET ME OUT!");
       greet.setBackground(Color.black);
       greet.setForeground(Color.red);
       greet.setFont( new Font("SansSerif", Font.BOLD, 30) );
       getContentPane().add(greet);

       timer = new StopWatch();
       timer.setBackground(Color.white);
       timer.setForeground(Color.blue);
       timer.setOpaque(true);
       timer.setFont(new Font("Serif", Font.PLAIN, 20));
       getContentPane().add( timer );

       changeText = new JButton("Change Text in this Applet");
       getContentPane().add(changeText);

       changeText.addActionListener( new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  if (greet.getText().equals("PLEASE LET ME OUT!"))
                     greet.setText("Help!");
                  else
                     greet.setText("PLEASE LET ME OUT!");
                  timer.setText("Please click me.");
                  if (changeText.getText().equals("Change Back"))
                     changeText.setText("Change Text in this Applet");
                  else
                     changeText.setText("Change Back");
              }
          } );

   } // end init()

} // end class NullLayoutDemo