java changeevent_JavaBean PropertyChangeEvent 事件使用

In this example we'll listen to bean's property change event. We create a small bean namedMyBean, adds attributes and getter/setter. We want to know or to get notified when the bean property name is changed.

First we need the add aPropertyChangeSupportfield to the bean, with this object we will fire the property change event. When we need to listen for the change we have to create an implementation of aPropertyChangeListener. In this example we'll just use theMyBeanclass as the listener.

ThePropertyChangeListenerhas a method calledpropertyChangeand inside this method we'll implementing the code to get the event fired by thePropertyChangeSupport.

package org.kodejava.example.bean;

import java.beans.PropertyChangeSupport;

import java.beans.PropertyChangeListener;

import java.beans.PropertyChangeEvent;

import java.io.Serializable;

public class MyBean implements PropertyChangeListener, Serializable {

private Long id;

private String name;

private PropertyChangeSupport pcs = new PropertyChangeSupport(this);

public MyBean() {

pcs.addPropertyChangeListener(this);

}

/**

* This method gets called when a bound property is changed.

*

* @param evt A PropertyChangeEvent object describing the event source

* and the property that has changed.

*/

public void propertyChange(PropertyChangeEvent evt) {

System.out.println("Name = " + evt.getPropertyName());

System.out.println("Old Value = " + evt.getOldValue());

System.out.println("New Value = " + evt.getNewValue());

}

public static void main(String[] args) {

MyBean bean = new MyBean();

bean.setName("My Initial Value");

bean.setName("My New Value");

bean.setName("My Yet Another Value");

}

//~ --------------------------------------------- Bean's Getters and Setters

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

String oldValue = this.name;

this.name = name;

//

// Fires a property change event

//

pcs.firePropertyChange("name", oldValue, name);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavabeanJava中一种特殊的类,它具有以下特点: 1. 具有无参构造函数; 2. 属性使用私有化封装,提供getter和setter方法; 3. 实现java.io.Serializable接口,支持序列化。 通常情况下,Javabean被用于在不同的组件之间传递数据,例如在Java Web开发中,我们可以使用Javabean作为数据传输对象(DTO)。 在使用Javabean时,我们先定义一个Javabean类,其中包含一些私有属性、公有getter和setter方法,如下所示: ```java public class UserBean implements java.io.Serializable { private String name; private int age; public UserBean() {} public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` 在这个例子中,我们定义了一个名为UserBean的Javabean类,它包含了两个私有属性:name和age,以及对应的公有getter和setter方法。 使用Javabean的时候,我们可以实例化一个UserBean对象,并设置它的属性值,如下所示: ```java UserBean user = new UserBean(); user.setName("Tom"); user.setAge(18); ``` 然后,我们可以将这个UserBean对象传递给其他组件,例如Servlet、JSP等,在这些组件中,我们可以通过调用UserBean对象的getter方法,获取到它的属性值,如下所示: ```java out.println("Name: " + user.getName()); out.println("Age: " + user.getAge()); ``` 这样,我们就完成了Javabean使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值