1
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport; /**
* @author huhy
*
*/
public class MyBean { private String id;
private String name; private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
this); public MyBean(String id, String name) {
this.id = id;
this.name = name;
} public String getId() {
return id;
} public void setId(String id) {
String oldId = this.id;
this.id = id;
changeSupport.firePropertyChange("id", oldId, id);
} public String getName() {
return name;
} public void setName(String name) {
String oldName = this.name;
this.name = name;
changeSupport.firePropertyChange("name", oldName, name);
} public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
} public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
}
2
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField; public class Panel1 extends JPanel implements PropertyChangeListener {
private static final long serialVersionUID = 1L; private MyBean mybean; private JLabel nameLabel = new JLabel("name1"); private JTextField nameInput = new JTextField();; private JLabel idLabel = new JLabel("id1"); private JTextField idInput = new JTextField(); public Panel1(MyBean mybean) {
this.mybean = mybean;
mybean.addPropertyChangeListener(this);
initialized();
} private void initialized() { setBorder(BorderFactory.createTitledBorder("输出1"));
setLayout(new GridLayout(2, 2));
idInput.setText(mybean.getId());
nameInput.setText(mybean.getName());
add(idLabel);
add(idInput);
add(nameLabel);
add(nameInput); } @Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() == mybean) {
idInput.setText(mybean.getId());
nameInput.setText(mybean.getName());
} }
}
3
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField; public class Panel2 extends JPanel implements PropertyChangeListener {
private static final long serialVersionUID = 1L; private MyBean mybean; private JLabel nameLabel = new JLabel("name2"); private JTextField nameInput = new JTextField();; private JLabel idLabel = new JLabel("id2"); private JTextField idInput = new JTextField(); public Panel2(MyBean mybean) {
this.mybean = mybean;
mybean.addPropertyChangeListener(this);
initialized();
} private void initialized() {
setBorder(BorderFactory.createTitledBorder("输出2"));
setLayout(new GridLayout(2, 2));
idInput.setText(mybean.getId());
nameInput.setText(mybean.getName());
add(idLabel);
add(idInput);
add(nameLabel);
add(nameInput); } @Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getSource() == mybean) {
idInput.setText(mybean.getId());
nameInput.setText(mybean.getName());
} }
}
4
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField; /**
* @author huhy
*
*/
public class PropertyChangeSupportTest extends JFrame {
private MyBean mybean = new MyBean("123", "helloKitty"); private JPanel panel = new JPanel(new GridLayout(1, 3)); private JTextField id = new JTextField();
private JTextField name = new JTextField(); private JButton button = new JButton("click"); public PropertyChangeSupportTest() {
initialized();
setLayout(new BorderLayout());
panel.setBorder(BorderFactory.createTitledBorder("输入"));
add(panel, BorderLayout.NORTH);
JPanel panel1 = new Panel1(mybean);
panel1.setPreferredSize(new Dimension(50, 80));
JPanel panel2 = new Panel2(mybean);
panel2.setPreferredSize(new Dimension(50, 100));
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
} private void initialized() {
id.setText(mybean.getId());
name.setText(mybean.getName());
button.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
mybean.setId(id.getText());
mybean.setName(name.getText()); } });
panel.add(id);
panel.add(name);
panel.add(button); } public static void main(String args[]) {
PropertyChangeSupportTest test = new PropertyChangeSupportTest(); } }