Java examples for Swing:JTextField
Creating a JTextField Component
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JTextField; public class Main { public static void main(String[] args) throws Exception { JTextField textfield = new JTextField("Initial Text"); int cols = 30; textfield = new JTextField("Initial Text", cols); textfield.addActionListener(new MyActionListener()); }//from w w w .j a va2 s . co m } class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent evt) { JTextField textfield = (JTextField) evt.getSource(); System.out.println(textfield.getText()); } }