Java PropertyChangeSupport not working for different properties

I developed my own javaBean for Swing. Now I'm trying to catch two properties when changing using PropertyChangeListener.

The problem is that PropertyChangeSupport for one of my properties in JavaBean works fine, but doesn't seem to start propertyChange property for another declared property.

Let me give you some snippets 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 kind of code in Bean-Class, but I cut it so that it was clear. I am trying to catch these Changes properties 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
   }
  }
 }

. - PropertyChangeSupport Change startDay. endDay, setEndDay, , , Change . simple System.out.println() s, , :

  • PropertyChangeListener , startDay
  • setStartDay() setEndDay()
  • setEndDay(), propertyChangeEvent property endDay

, . , - .

+3
1

, , , endDay, ...

, , "old" "endDay" setEndDay...

+6

Source: https://habr.com/ru/post/1722574/


All Articles