Java Reference
In-Depth Information
Example
In this example, the application is going to act as a
PropertyChangeListener
, and so
must implement method
propertyChange
. The application frame must be registered as
a
PropertyChangeListener
for the bean by executing method
addPropertyChangeLis-
tener
on the bean, supplying an argument of
this
. When the String identifying the
animation changes (i.e., when the value of property
imageName
changes), a
PropertyChangeEvent
will be generated and method
propertyChange
will be
invoked. The simple action to be taken by this method will be to change the title
of the application frame to refl ect the change in property
imageName
. As usual,
the changes from the original version of the program will be shown in bold text.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import animBeans.AnimBean
4
;
public class AnimBeanApp3 extends JFrame
implements ActionListener,
PropertyChangeListener
{
private AnimBean
4
sequence;
private JPanel speedControl, imageControl;
private JLabel delayPrompt, imagePrompt;
private JTextField delay, imageName;
public static void main(String[] args)
{
AnimBeanApp
3
frame = new AnimBeanApp
3
();
frame.setSize(150,250);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public AnimBeanApp
3
()
{
sequence = new AnimBean
4
();
sequence.addPropertyChangeListener(this);
setTitle(sequence.getImageName());
speedControl = new JPanel();
delayPrompt = new JLabel("Delay(ms): ");
delay = new JTextField(4);
imageControl = new JPanel();
imagePrompt = new JLabel("Image: ");
imageName = new JTextField(8);
add(sequence, BorderLayout.NORTH);
speedControl.add(delayPrompt);
Search WWH ::
Custom Search