import java.awt.event.*; import javax.swing.*; public class MyListener extends JFrame { //create a JTextField JTextField textField = new JTextField("Press enter"); //constructor public MyListener() { //add the listener on JTextField textField.addActionListener(new ActionListener() { //capture the event on JTextField public void actionPerformed(ActionEvent e) { //get and display the contents of JTextField in the console System.out.println("Text=" + textField.getText()); } }); //add JTextField to the frame getContentPane().add(textField); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300,100); } public static void main(String[] args) { new MyListener().setVisible(true); } }