The class which processes the TextEvent should implement this interface.The object of that class must be registered with a component. The object can be registered using the addTextListener() method.

Interface declaration

Following is the declaration for java.awt.event.TextListener interface:

  1. public interface TextListener
  2. extends EventListener

Interface methods

S.N.Method & Description
1 void textValueChanged(TextEvent e) Invoked when the value of the text has changed.

Methods inherited

This interface inherits methods from the following interfaces:

  • java.awt.EventListener

TextListener Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui > AwtListenerDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtListenerDemo {
  7. private Frame mainFrame;
  8. private Label headerLabel;
  9. private Label statusLabel;
  10. private Panel controlPanel;
  11. private TextField textField;
  12.  
  13. public AwtListenerDemo(){
  14. prepareGUI();
  15. }
  16.  
  17. public static void main(String[] args){
  18. AwtListenerDemo awtListenerDemo = new AwtListenerDemo();
  19. awtListenerDemo.showTextListenerDemo();
  20. }
  21.  
  22. private void prepareGUI(){
  23. mainFrame = new Frame("Java AWT Examples");
  24. mainFrame.setSize(400,400);
  25. mainFrame.setLayout(new GridLayout(3, 1));
  26. mainFrame.addWindowListener(new WindowAdapter() {
  27. public void windowClosing(WindowEvent windowEvent){
  28. System.exit(0);
  29. }
  30. });
  31.  
  32. headerLabel = new Label();
  33. headerLabel.setAlignment(Label.CENTER);
  34. statusLabel = new Label();
  35. statusLabel.setAlignment(Label.CENTER);
  36. statusLabel.setSize(350,100);
  37.  
  38. controlPanel = new Panel();
  39. controlPanel.setLayout(new FlowLayout());
  40.  
  41. mainFrame.add(headerLabel);
  42. mainFrame.add(controlPanel);
  43. mainFrame.add(statusLabel);
  44. mainFrame.setVisible(true);
  45. }
  46.  
  47. private void showTextListenerDemo(){
  48. headerLabel.setText("Listener in action: TextListener");
  49.  
  50. textField = new TextField(10);
  51.  
  52. textField.addTextListener(new CustomTextListener());
  53. Button okButton = new Button("OK");
  54. okButton.addActionListener(new ActionListener() {
  55. public void actionPerformed(ActionEvent e) {
  56. statusLabel.setText("Entered text: "
  57. + textField.getText());
  58. }
  59. });
  60.  
  61. controlPanel.add(textField);
  62. controlPanel.add(okButton);
  63. mainFrame.setVisible(true);
  64. }
  65.  
  66. class CustomTextListener implements TextListener {
  67. public void textValueChanged(TextEvent e) {
  68. statusLabel.setText("Entered text: " + textField.getText());
  69. }
  70. }
  71. }

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

  1. D:\AWT>javac com\tutorialspoint\gui\AwtListenerDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AwtListenerDemo

Verify the following output

awt_textlistener.jpg