Search code examples
javaswinglistenerswingxjdatechooser

Any listeners available for JXMonthView's traversable buttons?


Working with JXMonthView, it is possible to set it as "Traversable," which means that left and right arrows appear next to the month name in the header. They allow for the user to select a different month to view, effectively traversing through the calendar year.

I would like to put an event listener on those arrows so that each time they are clicked, the flagged dates will be refreshed. Currently, this only happens when the user clicks on a date in JXMonthView, which changes the current selection, which fires the refresh. Does anyone know how to access those individual traversing buttons and add a listener to them?


Solution

  • You can listen to firstDisplayedDay property change.

    According to the source code, BasicMonthViewUI.nextMonth() or previousMonth() invokes monthView.setFirstDisplayedDay which in turn invokes firePropertyChange("firstDisplayedDay", oldDate, getFirstDisplayedDay());.

    For example:

    monthView.addPropertyChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent e) {
            if ("firstDisplayedDay".equals(e.getPropertyName())) {
                System.out.println("updated");
            }
        }
    });