简体   繁体   中英

Java PropertyChangeSupport does not fire for different properties

I have developed an own javaBean for Swing. Now I'm trying to catch two properties on change, using a PropertyChangeListener.

The problem is that the PropertyChangeSupport for one of my properties in the JavaBean works fine but it does not seem to fire any propertyChange for the other declared property.

Let me give you some extracts of my code:

JCalendar Bean:

public class JCalendar extends JPanel {  
  private int startDay, endDay;  
  private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);  

  public int getStartDay() {
    return startDay;
  }

  public void setStartDay(int startDay) {
    int old = this.startDay;  
    this.startDay = startDay;  
    this.pcs.firePropertyChange("startDay", old, startDay);
  }

  public int getEndDay() {
    return endDay;
  }

  public void setEndDay(int endDay) {
    int old = this.endDay;
    this.endDay = endDay;
    this.pcs.firePropertyChange("endDay", old, endDay);
  }
}

Of course there is some more code in the Bean-Class but I cut it in order to keep it clear. I try to catch these propertyChanges with a PropertyChangeListener in another class like this:

class markedDayListener implements PropertyChangeListener {

  public void propertyChange(PropertyChangeEvent arg0) {

   System.out.println(arg0.getPropertyName());

   if(arg0.getPropertyName().equals("startDay")) {
      // Do something
   } else if(arg0.getPropertyName().equals("endDay")) {
      // Do something
   }
  }
 }

So far everything seems correct to me. But for some reason the PropertyChangeSupport only fires a propertyChange when startDay is changed. As soon as I change endDay, the setEndDay Method is called but there seems no propertyChange to be fired. I have logged all the events with simple System.out.println()s in order to see, which methods are called and found out the following:

  • The PropertyChangeListener is added properly and works when startDay changes
  • the setStartDay() and setEndDay()-methods are called properly
  • when setEndDay() is called, no propertyChangeEvent with the propertySource "endDay" is set

I have been trying this for some hours now and don't see what's wrong. Hopefully someone here can help me.

If all you say is right the only explanation is that you set the same value for endDay again, thus no event is fired...

Add some traces to see if the values of 'old' and 'endDay' in method setEndDay...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM