Van icon

JAVA PropertyChange No Object

Links


    //CONSTANTES

    public class CONSTANTES {
        public  static final  String PROPIEDAD_A_COMPROBAR_SI_CAMBIA="VALOR SI O NO";
    }
    



    //AVISADOR 

    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeSupport;
    import java.io.Serializable;


    public class AvisadorBasic implements Serializable{
    private boolean encendido = false;
    private final PropertyChangeSupport propertySupport;
    
    public AvisadorBasic() {
        propertySupport = new PropertyChangeSupport(this);  // necesito crear el objeto que se encarga de avisar de los cambios..
    }

    public boolean isEncendido() {
        return encendido;
    }

    public void setEncendido(boolean nuevoValor) {
        if (nuevoValor == this.encendido) {
            return;  // nada que hacer ya que es false
        }
        this.propertySupport.firePropertyChange(CONSTANTES.PROPIEDAD_A_COMPROBAR_SI_CAMBIA, this.encendido , nuevoValor );/*valor anterior*//* valor nuevo*/
        this.encendido = nuevoValor; // el valor de encedido es el nuevo.
    }
    
    
    
    
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }

     //Metodo para quitar un escuchador: un seguidor .
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }
}



    //Escuchador
    
    
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.io.Serializable;
    import javax.swing.JLabel;

    public class EscuchadorBasic  extends JLabel implements Serializable{
    
    private AvisadorBasic AA_Avisador = null;
    PropertyChangeListener MiEscuchador;
    
    public EscuchadorBasic() {
        this.setText("Escuchador de no bjeto");

        this.MiEscuchador = new PropertyChangeListener() // definimos el objeto que atiente al evento.. algo parecido a los botones, pero ahora para este tipo de objeto.
        {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                cambioElEstado(evt);
            }
        };

    }
    
    public void cambioElEstado(PropertyChangeEvent evt) {
       
        boolean sino = (boolean) evt.getNewValue();
        if (sino) {
            this.setText("Activado");//
        } else {
            this.setText("NO ESTA ACTIVADO");
        }
            
        
    }

    public AvisadorBasic getAA_Avisador() {
        return AA_Avisador;
    }

    public void setAA_Avisador(AvisadorBasic AA_Avisador) {
       if (this.AA_Avisador != null) {
            this.AA_Avisador.removePropertyChangeListener(this.MiEscuchador);
        }

        this.AA_Avisador = AA_Avisador;

        // le decimos que me apunte a su lista de seguidores.
        if (this.AA_Avisador != null) {
            this.AA_Avisador.addPropertyChangeListener(this.MiEscuchador);
        }
    }

}