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
XWLAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bart CremersJava ArchitectCommented:
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
0
XWLAuthor Commented:
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
0
Bart CremersJava ArchitectCommented:
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.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

XWLAuthor Commented:
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
0
Bart CremersJava ArchitectCommented:
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.
0
Bart CremersJava ArchitectCommented:

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
 
public class Test {
 
    public static void main(String[] args) {
        JFrame f = new JFrame();
 
        TestPanel testPanel = new TestPanel();
 
        testPanel.addPropertyChangeListener("eventHappened", new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                System.out.println(evt);
            }
        });
        f.add(testPanel);
 
        f.pack();
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}
 
class TestPanel extends JPanel {
 
    private JCheckBox chk_EmailRequired;
 
    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) {
                boolean newValue = chk_EmailRequired.isSelected();
                firePropertyChange("eventHappened", !newValue, newValue);
            }
        });
        this.add(chk_EmailRequired);
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
XWLAuthor Commented:
It works. Thank you!

XWL
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.