Question

How i would add an action listener to the jcalendar? I want to get the date whenever you click in a day, so i will show the entire date on a jtextfield. I have tried something like this, but It does nothing when i click a day.

    cal = new JCalendar();
    cal.setWeekOfYearVisible(false);
    cal.getDayChooser().getDayPanel().addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent e) {
            System.out.println(e.getPropertyName()
                    + ": " + (Date) e.getNewValue());

        }
    });

I tried the code of the answer to this question too, that is similar, but nothing. Adding actionListener to jCalendar

Was it helpful?

Solution

I think you are adding the property change listener to the wrong bean. I inspected the JCalendar code and the getDayPanel() method returns just a regular JPanel that I don't think knows about the "day" property you are interested in.

 /**
 * Returns the day panel.
 * 
 * @return the day panel
 */
public JPanel getDayPanel() {
    return dayPanel;
}

I think you should add your property change listener to the daychooser itself, which is the class that knows about the "day" property. Also, you might want to register for the "day" property of the day chooser:

    cal = new JCalendar();
    cal.setWeekOfYearVisible(false);
    cal.getDayChooser().addPropertyChangeListener("day", new PropertyChangeListener() {

    @Override
    public void propertyChange(PropertyChangeEvent e) {
        System.out.println(e.getPropertyName()
                + ": " + e.getNewValue());

    }
});

Still, that will only give you the day that the user picked, not the entire date.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top