With the property-change mechanism incorporated into the form, you can define a form listener to be activated by property-change events.
That listener implements the PropertyChangeListener
interface, which
requires the definition of the propertyChange
method and the
importation of the java.beans
package.
The propertyChange
method determines the property name from an
instance of the PropertyChangeEvent
class using the
getPropertyName
method. Then, acting typically, the
propertyChange
method fetches information from the form and relays
that information to a model. The constructor ensures that the required
movie application is in hand, ready to produce the currently selected movie
model:
import java.beans.*; public class RatingPanelListener implements PropertyChangeListener { private MovieApplication applet; public RatingPanelListener(MovieApplication a) { applet = a; } public void propertyChange (PropertyChangeEvent e) { String property = e.getPropertyName(); if (applet.getMovie() instanceof Movie) { if (property.equals("value1")) { applet.getMovie().setScript(applet.getForm().getValue1()); } else if (property.equals("value2")) { applet.getMovie().setActing(applet.getForm().getValue2()); } else if (property.equals("value3")) { applet.getMovie().setDirection(applet.getForm().getValue3()); } } } }