Share this page 

Reset all textfields in one shotTag(s): AWT


 import java.applet.*;    
 import java.awt.*;
 import java.awt.event.*;

 public class FirstApplet extends Applet implements
     ActionListener {
  TextField t1;
  TextField t2;
  TextField t3;
  TextField t4onPanel;
  Panel p1;
  Button b;

  public void init() {
   add(t1= new TextField(20));
   add(t2= new TextField(20));
   add(t3= new TextField(20));
   t4onPanel = new TextField(20);
   p1 = new Panel();
   p1.setBackground(new Color(0).yellow);
   p1.add(t4onPanel);
   add(p1);
   add(b = new Button("reset TextFields"));
   b.addActionListener(this);
   }

  public void actionPerformed(ActionEvent ae) {
   if (ae.getSource() == b)
     resetTextFields(this);
   }
   
  public static void resetTextFields(Container c) {
   Component [] components = c.getComponents();
   for (int i = 0; i < components.length; i++ ) {
     if (components[i] instanceof Container)
       resetTextFields((Container) components[i]) ; 
     else if (components[i] instanceof TextField) 
       ((TextField) components[i]).setText("") ;
     }
   }
  }

<HTML><HEAD></HEAD><BODY>
 <APPLET CODE="FirstApplet.class"  
         NAME="myApplet" 
         HEIGHT=200 WIDTH=200>
 </APPLET></BODY></HTML>