1
2
3
4
5
6
7
8
9 package com.builder.uk.watchme;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 import javax.swing.*;
33 import java.awt.*;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.WindowAdapter;
37 import java.awt.event.WindowEvent;
38 import java.beans.PropertyChangeEvent;
39 import java.beans.PropertyChangeListener;
40
41
42
43
44 public class WatchMeFrame extends JFrame implements PropertyChangeListener {
45 WatchMeBean watchMeBean;
46
47 JLabel count;
48 JLabel msg;
49
50 public WatchMeFrame() {
51 super("WatchMe");
52
53 this.setLayout(new GridLayout(3, 2));
54
55 count = new JLabel("");
56 msg = new JLabel("");
57
58 this.add(new JLabel("Count:"));
59 this.add(count);
60 this.add(new JLabel("Message:"));
61 this.add(msg);
62
63 JButton inc = new JButton("Increment");
64
65 this.add(inc);
66
67
68 this.addWindowListener
69 (
70 new WindowAdapter() {
71
72
73
74 public void windowClosing(WindowEvent e) {
75 WatchMeFrame.this.windowClosed();
76 }
77 }
78 );
79
80
81 inc.addActionListener
82 (
83 new ActionListener() {
84 public void actionPerformed(ActionEvent e) {
85 WatchMeFrame.this.buttonPressed();
86 }
87 }
88 );
89 }
90
91 public void setWatchMeBean(WatchMeBean watchMeBean) {
92 this.watchMeBean = watchMeBean;
93 count.setText(Integer.toString(watchMeBean.getCount()));
94 msg.setText(watchMeBean.getMsg());
95 watchMeBean.addPropertyChangeListener(this);
96 }
97
98 protected void buttonPressed() {
99 if (watchMeBean != null) {
100 watchMeBean.incCount();
101 }
102 }
103
104 protected void windowClosed() {
105 System.exit(0);
106 }
107
108 public void propertyChange(PropertyChangeEvent evt) {
109 String propname = evt.getPropertyName();
110
111 if (propname.equals("msg")) {
112 msg.setText((String) evt.getNewValue());
113 } else if (propname.equals("count")) {
114 count.setText(((Integer) evt.getNewValue()).toString());
115 }
116 }
117
118
119 }