Wednesday, June 6, 2007

JCOMPONENT PROPERTIES.

JCOMPONENT PROPERTIES.

All swing components conform to the javabeans specification. The javabeans specification will be discussed later. Among the five features a javabean is expected to support are a set of properties and associated accessor methods for them.

Property: a property is a global variable. Accessor methods, if any, of a property are of the setPropertyName(), getPropertyName(), or isPropertyName() methods.

a. Simple, bound and constrained properties.

A simple property is a property that has no event firing associated with a change in its value.

A bound property is a property from which a property change event is/are fired after it changes state. We can register propertyChangeListeners to listen for propertyChangeEvents through JComponent's addPropertyChangeListener method.

Constrained porperty is a property for which propertyChangeEvents are fired before a change in state occurs. We can register vetoableChangeListeners to listen for propertyChangeEvents through JComponent's addVetoableChangeListener method. A change can be vetoed in the event handling code of a vetoableChangeListener by throwing a propertyVetoException. Only one swing class has constrained properties, the JInternalFrame class.

PropertyChangeEvents carry three pieces of information: name of the property, old value and new values. Beans can use an instance of propertyChangeSuppport to manage the dispatching of propertyChangeEvents corresponding to each bound property to each registered listener. Also, an instance of vetoabableChangeSupport can be used to manage the sending of all propertyChangeEvents corresponding to each constrained property. There is a class in swing, swingPropertyChangeSupport in the javax.swing.events package that extends the propertyChangeSupport class and provides more efficient methods.

Code for a class that demonstrates bound properties.

import javax.swing.*;
import java.awt.event.*;

public class Property extends JPanel implements ActionListener{

JButton jButton1 = new JButton(" ");
JTextField jText;
/** Creates a new instance of Property */
public Property() {
jButton1 = new JButton("button1");
add(jButton1);
jButton1.addActionListener(this);
jText = new JTextField("welcome", 20);
add(jText);

}

private static void dButton(){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Property");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Property nProp = new Property();
nProp.setOpaque(true);
frame.setContentPane(nProp);
frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e){
jText.setText(jButton1.getText());
}

public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
dButton();
}
});
}
}

b. Change properties.

Change properties carry information about the source of a change. We use changeListeners to listen for changeEvents that get fired when these properties change state. ChangeEvents carry only one information, the source of the event.

c. client property
this property is implemented on a list such that for every propertyChange associated with a propertyChangeEvent, a list of propertyChangeListeners sought to notify all listeners for that event that a change has occurred.

Properties of swing components will be discussed in the course of these discussion.

No comments: