20-CS-694 Advanced Programming Techniques Spring 2012
Widgets

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous      Applet Source      All lectures

JTextField Test

Write some text into the text field at the top, then click on the center button to see the text move to the lower text field.

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

public class TextApplet extends Applet implements ActionListener {
   JTextField t, s;
   JButton b;
   String value = "This is a string";

   public void init () {
      Panel p = new Panel();
      p.setLayout(new GridLayout(3,1,10,10));
      p.add(t = new JTextField(value,40));
      Panel q = new Panel();
      q.setLayout(new GridLayout(1,3));
      q.add(new JLabel(""));
      q.add(b = new JButton("Set Text"));
      q.add(new JLabel(""));
      p.add(q);
      p.add(s = new JTextField(40));
      add("Center", p);
      s.setEditable(false);
      b.addActionListener(this);
   }

   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == b) s.setText(t.getText());
   }
}