/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. //=== BeanUser.java ==== 

2. package com.javalessons.beanlisteners; 

3. import java.awt.*; 

4. import java.awt.event.WindowAdapter

5. import javax.swing.JFrame

6.  

7. public class BeanUser extends JFrame

8.  

9.   BeanOne bone = new BeanOne(); 

10.   BeanTwo btwo = new BeanTwo(); 

11.   ListenUp somecomp = new ListenUp(); 

12.    

13.   public BeanUser() { 

14.     Container cp = this.getContentPane(); 

15.     cp.setLayout(new GridLayout(0,2)); 

16.     cp.add(bone); 

17.     cp.add(btwo); 

18.      

19.     bone.addPropertyChangeListener(btwo); 

20.     bone.addVetoableChangeListener(somecomp); 

21.     bone.setProp( 246 ) ; 

22.   } 

23.  

24.   public static void main(String[] args) { 

25.     BeanUser ap = new BeanUser(); 

26.     ap.setSize(new Dimension(330,150)); 

27.     ap.addWindowListener( 

28.         new WindowAdapter(){ 

29.           public void windowClosing(java.awt.event.WindowEvent wev) { 

30.             System.exit(0); 

31.           }; 

32.     }); 

33.     ap.setVisible(true); 

34.   } 

35.

36. //=== BeanOne.java ==== 

37. package com.javalessons.beanlisteners; 

38. import java.beans.*; 

39. import javax.swing.JLabel

40. import javax.swing.JPanel

41. import javax.swing.SwingUtilities

42.  

43. public class BeanOne extends JPanel

44.  

45.   private int prop = 0

46.   private PropertyChangeSupport pcs; 

47.   private VetoableChangeSupport vcs; 

48.   private JLabel lb=new JLabel(); 

49.    

50.   public BeanOne() { 

51.     add(lb); 

52.     lb.setText("BeanOne's property : " + prop ); 

53.     pcs = new PropertyChangeSupport(this); 

54.     vcs = new VetoableChangeSupport(this); 

55.   } 

56.   public void setProp(int p) { 

57.     if (p<0 || p>255) return;  

58.     int old = prop; 

59.     try

60.       vcs.fireVetoableChange("prop", new Integer(old), new Integer(p) ); 

61.       pcs.firePropertyChange("prop", new Integer(old), new Integer(p) ); 

62. //    this.firePropertyChange("prop", new Integer(old), new Integer(p) ); 

63.          

64.       prop = p; 

65.       if ( SwingUtilities.isEventDispatchThread() ) 

66.         lb.setText("BeanOne's property : " + prop ); 

67.       else

68.        SwingUtilities.invokeLater( 

69.         new Runnable(){ 

70.          public void run(){ 

71.           try

72.             lb.setText("BeanOne's property : " + prop ); 

73.           }catch(Exception e){ e.printStackTrace(); } 

74.          } 

75.        } );  } 

76.     } 

77.     catch (PropertyVetoException exc) { 

78.       System.out.println("-- prop change vetoed --"); 

79.       exc.printStackTrace(); 

80.     } 

81.     } 

82.  

83.   public int getProp() { return prop; } 

84.  

85.   public synchronized void 

86.   addPropertyChangeListener( PropertyChangeListener l ) { 

87.    pcs.addPropertyChangeListener(l); 

88.   } 

89.   public synchronized void 

90.   removePropertyChangeListener( PropertyChangeListener l ) { 

91.    pcs.removePropertyChangeListener(l); 

92.    } 

93.  

94. //addPropertyChangeListener( String propname, PropertyChangeListener l ) 

95.    

96.   public synchronized void 

97.   addVetoableChangeListener(VetoableChangeListener l) { 

98.     vcs.addVetoableChangeListener(l); 

99.   } 

100.   public synchronized void 

101.   removeVetoableChangeListener(VetoableChangeListener l) { 

102.     vcs.removeVetoableChangeListener(l); 

103.   } 

104.   public PropertyChangeListener[] getPropertyChangeListeners() { 

105.     return pcs.getPropertyChangeListeners(); 

106.   } 

107.   public VetoableChangeListener[] getVetoableChangeListeners() { 

108.     return vcs.getVetoableChangeListeners(); 

109.   } 

110.

111. //=== BeanTwo.java ==== 

112. package com.javalessons.beanlisteners; 

113. import java.beans.PropertyChangeEvent

114. import java.beans.PropertyChangeListener

115. import javax.swing.JLabel

116. import javax.swing.JPanel

117. import javax.swing.SwingUtilities

118.  

119. import java.awt.Color

120.  

121. public class BeanTwo extends JPanel 

122.   implements PropertyChangeListener

123.  

124.   Color bg = Color.WHITE

125.   public BeanTwo() { add(new JLabel("BeanTwo")); } 

126.  

127.   public void propertyChange( PropertyChangeEvent pcev ) { 

128.     if ( !pcev.getPropertyName().equals("prop")) return

129.     if ( !(pcev.getSource() instanceof BeanOne) ) return

130.     int val = ( (Integer)pcev.getNewValue() ).intValue(); 

131.     if (val<0 || val>255) return

132.     setBg( new Color( 0, val, val ) ); 

133.   } 

134.   public Color getBg() { 

135.     return bg; 

136.   } 

137.   public void setBg(Color c) { 

138.     this.bg = c; 

139.      

140.     if ( SwingUtilities.isEventDispatchThread() ) 

141.       this.setBackground(bg); 

142.     else

143.      SwingUtilities.invokeLater( 

144.       new Runnable(){ 

145.        public void run(){ 

146.         try

147.           BeanTwo.this.setBackground(bg); 

148.         }catch(Exception e){ e.printStackTrace(); } 

149.       } 

150.      } );  } 

151.   } 

152.

153. //=== ListenUp.java ==== 

154. package com.javalessons.beanlisteners; 

155. import java.beans.*; 

156.  

157. public class ListenUp 

158.   implements VetoableChangeListener

159.  

160.   public void vetoableChange( PropertyChangeEvent pcev ) 

161.     throws PropertyVetoException

162.     if ( !pcev.getPropertyName().equals("prop") ) return

163.     if ( !(pcev.getSource() instanceof BeanOne) ) return

164.     int val = ( (Integer)pcev.getNewValue() ).intValue(); 

165.     if ( val < 120

166.       throw new PropertyVetoException("Too low", pcev); 

167.   } 

168.

169.