Developing Java Beans // the decrement button protected
Developing Java Beans public void handleDecrement(ActionEvent evt) { // get the current value and subtract 1 int val = primaryLabel.getValue() - 1; // try to set the new value try { primaryLabel.setValue(val); } catch (PropertyVetoException e) { } } // handle the increment button push public void handleIncrement(ActionEvent evt) { // get the current value and add 1 int val = primaryLabel.getValue() + 1; // try to set the new value try { primaryLabel.setValue(val); } catch (PropertyVetoException e) { } } } When the decrement button is pressed, the handleDecrement() method gets called. The Value property of the primary NumberLabel object is retrieved and its value is decremented and stored locally. Then handleDecrement() attempts to set the Value property to this new value. The PropertyVetoException must be caught in case the change is vetoed. When the increment button is pressed, the handleIncrement() method does the same thing, except that the value is incremented instead. When the applet’s init() method is called, the buttons and labels are added first. Next, the instance of the Constrainer class is registered as a listener for VetoableChange events from the primary NumberLabel object, and the other NumberLabel object (mirrorLabel) is registered to listen for PropertyChange events. The button adapter is set up to route action events, and the two labels are initialized with two different values. Figure 4.4 shows what the applet looks like when it first starts up. The components are labeled with the variable names to show where they appear on the applet. Figure 4.4. An applet using a constrained property: initial state When the decrement (<<) and increment (>>) buttons are pressed, both labels update together. Press the buttons and watch how these two labels stay in sync by setting the Value property of one of page 69
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services