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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Bart CremersConnect With a Mentor Java 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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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
 
XWLAuthor Commented:
It works. Thank you!

XWL
0
All Courses

From novice to tech pro — start learning today.