XWL
asked on
firePropertyChange does not work
Hi All,
I am having a problem: firePropertyChange does not trigger event handler function. Could anybody here gice me an idea to resolve it?
My system: JDK 1.5.0_12 in Eclipse on Windows XP/SP2
In order to make my event control class response to an event, I did these:
1) In event control class:
Public class EventCotrol implements PropertyChangeListener {
public EventCotrol (TestPanel panel) {
panel.addPropertyChangeLis tener("eve ntHappened ", this);
}
public void propertyChange(PropertyCha ngeEvent evt) {
// do something useful according to data of evt
}
}
2) In event class:
public class EventTestor extends BoundPropertyBean {
Boolean eventHappened = false;
public void setEventHappended(boolean happened) {
boolean old = happened;
eventHappened = happened;
if (happened != old)
firePropertyChange(("event Happened", old, eventHappened);
}// End of fireEvent(boolean happened)
}// End of class EventTestor
3) In JPanel class:
Public class TestPanel extends JPanel {
Private EventTestor testor = new EventTestor ();
// this should trigger the EventControllor's propertyChange method is called. However, the method does not get called
testor. setEventHappended (true);
}// End of class MyPanel
I guess I have something wrong with registering the PropertyChangeListener. However, I cannot figure it out.
Thank you,
XWL
I am having a problem: firePropertyChange does not trigger event handler function. Could anybody here gice me an idea to resolve it?
My system: JDK 1.5.0_12 in Eclipse on Windows XP/SP2
In order to make my event control class response to an event, I did these:
1) In event control class:
Public class EventCotrol implements PropertyChangeListener {
public EventCotrol (TestPanel panel) {
panel.addPropertyChangeLis
}
public void propertyChange(PropertyCha
// do something useful according to data of evt
}
}
2) In event class:
public class EventTestor extends BoundPropertyBean {
Boolean eventHappened = false;
public void setEventHappended(boolean happened) {
boolean old = happened;
eventHappened = happened;
if (happened != old)
firePropertyChange(("event
}// End of fireEvent(boolean happened)
}// End of class EventTestor
3) In JPanel class:
Public class TestPanel extends JPanel {
Private EventTestor testor = new EventTestor ();
// this should trigger the EventControllor's propertyChange method is called. However, the method does not get called
testor. setEventHappended (true);
}// End of class MyPanel
I guess I have something wrong with registering the PropertyChangeListener. However, I cannot figure it out.
Thank you,
XWL
ASKER
Hi Bart_Cr,
Thank you for your link. I understand the event mechanism. My question is how to register PropertyChangeListener in my case, which is not mentioned in the link.
Does anybody know how to do it in my case?
Thanks,
WL
Thank you for your link. I understand the event mechanism. My question is how to register PropertyChangeListener in my case, which is not mentioned in the link.
Does anybody know how to do it in my case?
Thanks,
WL
There's not enough code available to figure it out. You've got the EventControl class which implements the listener and in the constructor you correctly add the listener on a Panel for the property "eventHappened". Then you've got a BoundPropertyBean subclass where you fire the event. What's missing here is a link between the bean and the panel.
If you register the listener on a panel you'll need to fire the event on that same panel.
If you register the listener on a panel you'll need to fire the event on that same panel.
ASKER
Here is the panel class:
public class TestPanel extends JPanel {
private JCheckBox chk_EmailRequired;
Private EventTestor testor = new EventTestor ();
public TestPanel() {
super(new BorderLayout());
initComponents();
}
private void initComponents() {
chk_EmailRequired = new JCheckBox("Email required");
chk_EmailRequired.addChang eListener( new ChangeListener() {
public void stateChanged(ChangeEvent e) {
testor.setEventHappended(( (JCheckBox )e.getSour ce()).isSe lected());
}
});
this.add(chk_EmailRequired );
}
}
Thta's all I can think about.
Any idea?
Thank you Bart_Cr!
XWL
public class TestPanel extends JPanel {
private JCheckBox chk_EmailRequired;
Private EventTestor testor = new EventTestor ();
public TestPanel() {
super(new BorderLayout());
initComponents();
}
private void initComponents() {
chk_EmailRequired = new JCheckBox("Email required");
chk_EmailRequired.addChang
public void stateChanged(ChangeEvent e) {
testor.setEventHappended((
}
});
this.add(chk_EmailRequired
}
}
Thta's all I can think about.
Any idea?
Thank you Bart_Cr!
XWL
Somewhere you need new EventControl(testPanel); to get the propertyChangeListener registered on the testPanel. And then you need to fire a propertyChangeEvent on the TestPanel.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works. Thank you!
XWL
XWL
http://java.sun.com/docs/books/tutorial/uiswing/events/index.html