import sunw.demo.buttons.OurButton; import java.beans.*; import java.awt.*; import java.awt.event.*; import java.io.*; import utilities.*; public class PropertyChangeDemo extends Frame implements PropertyChangeListener { protected OurButton aButtonBean; public static void main (String[] args) { new PropertyChangeDemo(); } //------------------------------------------------------------------- // Constructor: create window with single bean button //------------------------------------------------------------------- public PropertyChangeDemo() { super("Just sit back and watch.."); setLayout(new FlowLayout()); // instantiate a test bean (never with "new" !!) try { aButtonBean = (sunw.demo.buttons.OurButton) Beans.instantiate( null, "sunw.demo.buttons.OurButton"); } catch (ClassNotFoundException noSuchBean) { System.out.println("OurButton bean not found: "+ noSuchBean); System.exit(10); } catch (IOException beanIOerror) { System.out.println("I/O error while loading bean: " + beanIOerror); System.exit(10); } // unlike normal Buttons, we cant give an OurButton an initial label, // so set the label now. aButtonBean.setLabel("When my properties change..."); add(aButtonBean); // register us as interested in button property changes aButtonBean.addPropertyChangeListener(this); // pop open window with button setSize(new Dimension(300,75)); setVisible(true); // wait a bit, then modify one of the button's properties System.out.println("No need to push that button.. I'll do that."); MiscKit.delay(6*1000); aButtonBean.setLabel("anyone can know about it !"); MiscKit.delay(12*1000); // close window, quit setVisible(false); System.exit(0); } //------------------------------------------------------------------- // propertyChange() is the method we have to implement from interface // PropertyChangeListener. This is where we hear of OurButton properties // changing. //------------------------------------------------------------------- public void propertyChange(PropertyChangeEvent pcEvent) { String changedProperty; Object oldPropertyValue, newPropertyValue; changedProperty = pcEvent.getPropertyName(); oldPropertyValue = pcEvent.getOldValue(); newPropertyValue = pcEvent.getNewValue(); System.out.println("OurButton property '"+ changedProperty +"'"); System.out.println(".. changed from '"+ oldPropertyValue +"'"); System.out.println(".. to '"+ newPropertyValue +"'"); } } // End of Class PropertyChangeDemo