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

import java.awt.event.*;

import java.util.Observable;
import java.util.Observer;


// JTextdoc Listener
import javax.swing.event.*;



public class Test_Listener_TextField extends JFrame implements ActionListener, ITextField_Change {

  JLabel jLabel1 = new JLabel();
  JTextField TName = new JTextField();
  JLabel jLabel2 = new JLabel();
  JTextFieldDocListener TName2;

  JButton BnOk = new JButton();
  JButton BnESC = new JButton();

  public Test_Listener_TextField() {
    setSize(400, 230);
    setTitle("Test Test_Listener_TextField");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    this.getContentPane().setLayout( new GridBagLayout() );
    TName2 =  new JTextFieldDocListener(this,"mein Text");

    jLabel1.setText("A");
    TName.setText("-");

    jLabel2.setText("ABCDEFGHIJKL");

    jLabel1.setFont( new Font("Arial", Font.BOLD,18)  );
    TName.setFont( new Font("Arial", Font.BOLD,18)  );
    jLabel2.setFont( new Font("Arial", Font.BOLD,18)  );
    TName2.setFont( new Font("Arial", Font.BOLD,18)  );

    BnOk.setText("Ok");
    BnESC.setText("Abbruch");
    BnOk.setFont( new Font("Arial", Font.BOLD,18)  );
    BnESC.setFont( new Font("Arial", Font.BOLD,18)  );

    int w=BnESC.getWidth();
    int h=BnESC.getHeight();
    BnOk.setPreferredSize( BnESC.getPreferredSize() );

    this.getContentPane().add(jLabel1,
         new GridBagConstraints(
             0, 0, 1, 1, 20.0, 0.0
            ,GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL,
            new Insets(10,25,40,0), 0, 0));

    this.getContentPane().add(TName,
        new GridBagConstraints(
               1, 0, 1, 1, 80.0, 0.0
            ,GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL,
            new Insets(10,25,40,25), 0, 0));


    this.getContentPane().add(jLabel2,
         new GridBagConstraints(
             0, 1, 1, 1, 20.0, 0.0
            ,GridBagConstraints.WEST,
            GridBagConstraints.HORIZONTAL,
            new Insets(0,25,40,0), 0, 0));

    this.getContentPane().add(TName2,
        new GridBagConstraints(
             1, 1, 1, 1, 80.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(0,25,40,25), 0, 0));



    JPanel pn = new JPanel();
    pn.add(BnOk);
    pn.add(BnESC);
    this.getContentPane().add(pn,
           new GridBagConstraints(0, 2, 2, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(0,0,0,0), 0, 0));

    BnOk.addActionListener(this);
    BnESC.addActionListener(this);


  }  // setGUI

   // wird vom Listener aufgerufen // callBack
  public void sendChangeMessage() {
      System.out.println(" Aufruf von sendChangeMessage");
  } // sendChangeMessage


    @Override
  public void actionPerformed(ActionEvent e) {
     if ( e.getSource() == BnESC) {
        System.exit(0);
     }
     if ( e.getSource() == BnOk) {
        
     }
  }

  public static void main(String[] args) {
    Test_Listener_TextField frame = new Test_Listener_TextField();
    frame.setVisible(true);
 }

}

interface ITextField_Change {

  public void sendChangeMessage();

}

// JTextdoc Listener
// import javax.swing.event.*;

class JTextFieldDocListener extends JTextField   {
   private ITextField_Change _parent;
   public  boolean bModify=false;

   public JTextFieldDocListener(ITextField_Change parent) {
     super();
     init(parent);
   }


   public JTextFieldDocListener(ITextField_Change parent, String sText) {
     super(sText);
     init(parent);
   }

   private void init(ITextField_Change parent) {
     _parent = parent;
       // ableiten der Update-Methoden des Listeners
     this.getDocument().addDocumentListener(new DocumentListener(){
         public void changedUpdate(DocumentEvent ev) {
             System.out.println("changedUpdate");
             _parent.sendChangeMessage();
         }
         
         public void insertUpdate(DocumentEvent ev) {
             System.out.println("changed insert");
             _parent.sendChangeMessage();
         }
         
         public void removeUpdate(DocumentEvent ev) {
             System.out.println("changed remove");
             _parent.sendChangeMessage();
         }
     });     
    } // init
}  // JTextFieldDocListener

