Interfaccia di AWT TextListener

La classe che elabora TextEvent dovrebbe implementare questa interfaccia. L'oggetto di quella classe deve essere registrato con un componente. L'oggetto può essere registrato utilizzando il metodo addTextListener ().

Dichiarazione dell'interfaccia

Di seguito è riportata la dichiarazione per java.awt.event.TextListener interfaccia:

public interface TextListener
   extends EventListener

Metodi di interfaccia

SNMetodo e descrizione
1

void textValueChanged(TextEvent e)

Richiamato quando il valore del testo è cambiato.

Metodi ereditati

Questa interfaccia eredita i metodi dalle seguenti interfacce:

  • java.awt.EventListener

TextListener Example

Crea il seguente programma java usando qualsiasi editor di tua scelta, diciamo D:/ > AWT > com > tutorialspoint > gui >

AwtListenerDemo.java
package com.tutorialspoint.gui;

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

public class AwtListenerDemo {
   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;
   private TextField textField;

   public AwtListenerDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtListenerDemo  awtListenerDemo = new AwtListenerDemo();  
      awtListenerDemo.showTextListenerDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
   
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showTextListenerDemo(){
      headerLabel.setText("Listener in action: TextListener");      

      textField  = new TextField(10);

      textField.addTextListener(new CustomTextListener());
      Button okButton = new Button("OK");
      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            statusLabel.setText("Entered text: " 
            + textField.getText());                
         }
      });

      controlPanel.add(textField);
      controlPanel.add(okButton);    
      mainFrame.setVisible(true);  
   }

   class CustomTextListener implements TextListener {
      public void textValueChanged(TextEvent e) {
         statusLabel.setText("Entered text: " + textField.getText());               
      }
   }
}

Compilare il programma utilizzando il prompt dei comandi. Vai aD:/ > AWT e digita il seguente comando.

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

Se non viene visualizzato alcun errore, significa che la compilazione è riuscita. Esegui il programma utilizzando il seguente comando.

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

Verificare il seguente output