JavaBean's property JavaBean's properties and properties referred to in the general Java program, or the properties of objects in all object-oriented programming languages are a concept, and the specific embodiment in the program is the variables in the class. In JavaBean design, subdivision is divided into four categories according to the different properties: Simple, INDEX, Bound and constrained properties. 3.1.1 Simple Attribute A simple property represents a variable accompanying a pair of GET / SET methods (C language process or functions called "method" in the Java program). Attribute name and the GET / SET method name associated with this property. For example: If there is a setx and getx method, you have a property named "X". If there is a method name ISX, it usually fails to "X" is a Boolean property (ie the value of X is True or false). For example, in this program: public class aldenen1 extends can (string ourstring = "hello"; // Attribute name is ourstring, type is string public aldenn1 () {// Alden1 () is Alden1 constructor, with C The meaning of the constructor is the same setBackground; setForeground (color.blue);} / * "set" attribute * / public void setstring (String newstring) {oorstring = news;} / * "get" attribute * / public String getString () {return ourstring;}} 3.1.2 indexed property A indexed property represents an array value. Use the SET / GET method corresponding to this property to get the value in the array. This property can also set or achieve the value of the entire array. Example: Public class aldenen2 extends canvas {int [] dataset = {1, 2, 3, 4, 5, 6}; // DataSet is an indexed property public aldenen2 () {setBackground (Color.red); setForeground (Color. Blue);} / * Set the entire array * / public void setDataSet (int [] x) {dataset = x;} / * Set a single element value in the array * / public void setDataSet (int index, int x) {dataset [ Index] = x;} / * acquire the entire array value * / public int [] getDataSet () {return dataset;} / * get the specified element value in the array * / public int getDataTaset (INT X) {Return Dataset [x] }} 3.1.3 Bound attribute A bound property is that other objects are to be notified when the value of the property changes. When each attribute value changes, this property is ignited to a PropertyChange event (in the Java program, the event is also an object). The attribute name, the original value of the attribute, and the new value of the attribute change are encapsulated. Such an event is to other beans, as for the receiving event bean, should be defined by itself. Figure 3.1 is a simple Bound property diagram showing that when the pushbutton's Background property is bind with the Dialog's Background property, if the PushButton's Background property changes, the Dialog's Background property also changes the same.
Example: public class alden3 extends Canvas {String ourString = "Hello"; // ourString bound property is a private PropertyChangeSupport changes = new PropertyChangeSupport (this); / ** Note: Java is a pure object-oriented language, if you want to use some kind of The method must indicate which object of which object is to be used. In the following program, the method of ignition event is performed, and the method used in this operation is in the PropertyChangeSupport class. So the above declaration and instantiate a Changes object, and will use the Changes's FirePropertyChange method to ignit the property change event. * / Public void setString (string newString) {String oldString = ourString; ourString = newString; / * ourString attribute value has changed, then followed by firing property change event * / changes.firePropertyChange ( "ourString", oldString, newString); } public string getString () {return ourstring;} / ** The following code is used for development tools. We cannot predict that Alden3 will become an application with which other bean combines, which can not predict which other components are related to this change when Alden3 is related, so Alden3 bean wants to reserve some interfaces to development tools. Tools use these interfaces to mount other JavaBean objects with Alden3. * / Public void addPropertyChangeListener (PropertyChangeLisener l) {changes.addPropertyChangeListener (l);} public void removePropertyChangeListener (PropertyChangeListener l) {changes.removePropertyChangeListener (l);} by the above code, development tools changes the call to other methods addPropertyChangeListener register JavaBean In the listener queue L of the opstring property, L is a vector array that stores any Java objects. The development tool can also use the Changes's RemovePropertyChangeListener method to log out of the specified object from the L, so that the change of the Oudtring property of Alden3 is no longer related to this object. Of course, when the programmer is handwritten, these two methods can also be called directly to hill other Java objects with Alden3. 3.1.4 constrained property A JavaBean constrained property is that when the value of this property is changed, the other Java object that has established a certain connection with this attribute has changed the value of the value value. The listener of the constrained property prevents the value of this attribute value by throwing PropertyveToException. The process is shown in Figure 3.2: The constrained attribute in the following program is PriceinCents.
public class JellyBean extends Canvas {private PropertyChangeSupport changes = new PropertyChangeSupport (this); private VetoableChangeSupport Vetos = new VetoableChangeSupport (this); / * aforementioned changes the same manner as in Example Vetos VetoableChangeSupport object may be used, to prevent the specific conditions down PriceInCents Value changes. * / ...... Public void setPriceinCents (int newpriceincents) throws propertyvetoException {/ * Method name The role of THROWS PropertyveToexception is to throw exceptions when there are other Java objects to veto the change of PriceNCents. * / / * To save the original property value * / int oldPriceInCents = ourPriceInCents; / ** ignition properties change veto event * / vetos.fireVetoableChange ( "priceInCents", new Integer (OldPriceInCents), new Integer (newPriceInCents)); / * * If there is any other object veto of the change of PriceINCENTS, the program throws an exception, no longer continues to perform the following two statements, the method ends. If no other objects rejected priceInCents change, then the following code ourPriceIncents assign a new value, and the ignition property change event * / ourPriceInCents = newPriceInCents; changes.firePropertyChange ( "priceInCents", new Integer (oldPriceInCents), new Integer (newPriceInCents ));} / ** is the same as the aforementioned Changes, and the interface is reserved for the PriceIncents property, so that other objects can be registered into the PriceINCENTS vetoing listener queue, or log out of the public void addvetoablechangelistener (VetoAbleChangelistener L) {Vetos .addvetoableChangeListener (L); Public Void RemoveToableVhangeListener (VetoableChangeListener L) {Vetos.RemoveveToableChangeListener (L);} ...} From the above example, a constrained property has two listeners: attribute changes Listels of the listener and the reed attribute change. The listener of the veto attribute changes to the corresponding control statement in its object code, and when the monitoring of the constrained property is changed, it is determined whether to veto this property value should be rejected in the control statement. In summary, whether a bean constrained property value can change depending on other beans or whether the Java object allows such changes. The allowable condition is defined in its own class by other beans or Java objects. JavaBean's event event processing is one of the core of the Javabean architecture. Through the event handling mechanism, some components can be made as an event source, and an event that can be described in the environment or other component can be issued. Thus, different components can be combined in the construction tool, and the components communicate between the components, constitute an application. Conceptually, an event is a transmission mechanism that changes between "source objects" and "listener objects".
There are many different purposes, such as mouse events, window boundaries, etc., and keyboard events, etc., for example, in Windows systems. In Java and JavaBean, it is defined a general, expandable event mechanism that provides a public framework for the definition and expansion of the model type and passing model, and is suitable for extensive applications. There is a high integration with Java language and the environment. The event can be described to capture and ignition. Other constructors can take some techniques to directly control events during design, as well as between event sources and event listeners. The event mechanism itself does not rely on complex development tools. In particular, it should also be that an event that can be generated by the specified object class can be found. It can be found that the specified object class can observe (listening) the event. Provide a regular registration mechanism that allows the relationship between the dynamic manipulation of the event source and event listener. No need other virtual machines and languages can be implemented. Efficient event delivery between event sources and listeners. A neutral mapping of the Javabean event model and the related other component architectural event model can be completed. 3.2.1 Overview The overall structural diagram of the JavabEan event model is shown in Figure 3.3, which is mainly configured: the event from the event source to the listener is conducted by the Java method call to the target listener object. A clear Java method is defined accordingly for each of the clear events. These methods are centralized in the event listener (EventListener interface, this interface should inherit Java.util.EventListener. A class that implements some or all of the methods in an event listener interface is an event listener. With the occurrence of the event, the corresponding state is usually packaged in the event status object, which must inherit from Java.util.EventObject. Event status objects are transmitted to the listener method that should respond to the event as a single ginseng. The identity of the event source that issues a certain particular event is that the design format of the specified design is the registration method for event listeners and accepts references to the specified event listener interface instance. Sometimes, event listeners cannot directly implement event listener interface, or other additionalctions, it is necessary to insert an instance of an event adapter class between a source and other one or more listeners to establish them. Contact. 3.2.2 Event State Object The status information related to the event is generally packaged in an event status object, which is a subclass of java.util.EventObject. According to the design habits, the names of this event status object class should be ended at Event. For example: public class MouseMovedExampleEvent extends java.util.EventObject {protected int x, y; / * create a mouse move event MouseMovedExampleEvent * / MouseMovedExampleEvent (java.awt.Component source, Point location) {super (source); x = location. X; Y = location.y;} / * Get mouse position * / public point getLocation () {Return New Point (x, y);}} 3.2.3 Event Loker Interface (EventListener Interface) and event monitors Due to Java The event model is based on method call, thus requiring a way to define and organize event handling methods. In javabean, event handling methods are defined in the EventListener interface of the Java.util.EventListener class, with the naming of the EventListener interface to end with Listener. Any class If you want to manipulate the method defined in the EventListener interface, you must perform this interface. This class is the event monitors.
For example: / * Define a mouse mobile event object * / public class mouseMoveDexampleEvent Extends java.util.EventObject {// In this class contains status information related to the mouse mobile event ...} / * Defines the mouse movement Event's listener interface * / interface mousemovedexamplelistener extends java.util.EventListener {/ * Defines how the mouse mobile event listener should support the method * / void mouseMovent MME);} Only the method is defined in the interface Name, method of parameters and return value types. Such as: The specific implementation of the mousemoved method in the above interface is defined in the ArbitraryObject class below. Class ArbitraryObject Implements MousemovedexampleListener {Public Void MouseMoved (MousemovedexampleEvent MME) {...}} ArbitraryObject is the monitors of the MouseMoveDexampleEvent event. 3.2.4 Event monitors Registration and logout For various possible event listeners to register their own events into the appropriate event sources, establish the event stream between the source and event listeners, the event source must provide registration and logout for event listeners Methods. This use process has been seen in the previous Bound property introduction, in practice, event listeners registration and logout To use standard design format: public void add
Development tools or programmers use these two ways to create event streams between source and listeners * / protected void notifymodelchanged () {/ ** Event source uses this method to notify the listener ActOlChanged event * / Vector L; EventObject E = New EventObject (this); / * First, copy the listener to the L array, freeze the status of EventListeners to pass the event. This way to ensure that the corresponding method of the target listener that has received the event is not effective before the event is passed to all listeners. * / Synchronized (this) {l = (vector) listeners.clone ();} for (int i = 0; i For example, following is a "button" of a user is Bean: public class OurButtonCustomizer extends Panel implements Customizer {... ... / * table when implementing the general properties such as OurButtonCustomizer, which must be implemented in addProperChangeListener and removePropertyChangeListener In this way, the constructor can use these functional code to add a listener to the attribute event. * / ... ... private PropertyChangeSupport changes = new PropertyChangeSupport (this); public void addPropertyChangeListener (PropertyChangeListener l) {changes.addPropertyChangeListener (l); public void removePropertyChangeListener (PropertyChangeListener l) {changes.removePropertyChangeListener (l);}. .. ... 3.3.2 PropertyEditor Interface A JavaBean provides a PropertyEditor class that creates an editor for the specified property. This class must inherit from Java.Beans.PropertyEditorsupport classes. The programmer of the constructor and the handwritten code does not use this class directly, but instantiates and calls this class in BeanInfo in the next section. Example: public class MoleculeNameEditor extends java.beans.PropertyEditorSupport {public String [] getTags () {String resule [] = { "HyaluronicAcid", "Benzene", "buckmisterfullerine", "cyclohexane", "ethane", "water"} }}}} The above example creates a property editor for the Tags property. In the constructor, the properties of the MoleculeName can be selected from the drop-down table should be "Hyaluronicaid" or "Water". 3.3.3.3BeanInfo Interface Each Bean class may have a related beanfo class associated with it, which describes the appearance of this bean when the construction tool appears. BeanInfo defines attributes, methods, events, displaying their names, providing simple help notes.