views:

149

answers:

1

Hey,

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.

+2  A: 

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...

pgras
Well, this is the perfect solution :)A deeper look at the Java API could have explained this as well but I would never have thought that I did not set a correct new value.Thank you very much for your fast help!
Ham
+1 Interesting. I'd always guarded against old value != new value in my code. Didn't realise that PropertyChangeSupport did this for you.
Adamski