Link to home
Start Free TrialLog in
Avatar of XWL
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.addPropertyChangeListener("eventHappened", this);
    }
   public void propertyChange(PropertyChangeEvent 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(("eventHappened", 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
Avatar of Bart Cremers
Bart Cremers
Flag of Belgium image

There's not enough info to help you fix your issue, but I guess you don't have a good grasp on how the event mechanism in Java works. Here's a link to get you started:

http://java.sun.com/docs/books/tutorial/uiswing/events/index.html
Avatar of XWL
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
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.
Avatar of XWL

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.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                testor.setEventHappended(((JCheckBox)e.getSource()).isSelected());
            }
        });
        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
Avatar of Bart Cremers
Bart Cremers
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XWL

ASKER

It works. Thank you!

XWL