How to set JCalendar date in JDateChooser

mfrancisp picture mfrancisp · Mar 27, 2015 · Viewed 13.2k times · Source

I have a JDateChooser that is used for selecting birth dates. It starts out as null date value and let's the user choose the birth date either using the textfield or the jdatechooser button which opens the Jcalendar.

What I'm trying to do is to set the JCalendar's date to -10 years from the current date. By default, the JCalendar is setting the date at the current year. I tried this code but it doesn't work -- it still sets the date to the current date. Any help will do. Thanks!

    dcBirthDateNew = new JDateChooser();
    dcBirthDateNew.setDateFormatString("MM/dd/yyyy");
    dcBirthDateNew.getDateEditor().getUiComponent().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent evt) {
            if (evt.getSource() instanceof JTextComponent) {
                final JTextComponent textComponent=((JTextComponent)evt.getSource());
                SwingUtilities.invokeLater(new Runnable(){
                    public void run() {
                        textComponent.selectAll();
                    }});
            }   
        }
    });
    JCalendar calx = dcBirthDateNew.getJCalendar();
    calx.setCalendar(getPrevDate());

private static Calendar getPrevDate() {
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    Date myDate = new Date(System.currentTimeMillis());
    System.out.println("result is "+ dateFormat.format(myDate));
    Calendar cal = Calendar.getInstance();
    cal.setTime(myDate);
    cal.add(Calendar.YEAR, -10);
    System.out.println(dateFormat.format(cal.getTime()));
    return cal;
}

Screenshot

Answer

trashgod picture trashgod · Mar 27, 2015

This seems to work correctly.

Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -10);

JDateChooser chooser = new JDateChooser(c.getTime());
this.add(chooser);

image

Addendum: The popup editor's date defaults to the value in the JDateChooser; if you set it explicitly, it will become the default value:

JDateChooser chooser = new JDateChooser();
chooser.getDateEditor().setDate(c.getTime());

You can update another, empty text field in a PropertyChangeListener, as shown here; the property name is "date".

Addendum: Looking closer, the JDateChooser implementation of ActionListener handles the default date described above. You can override it, replacing the lines shown below, to create a different default.

JDateChooser chooser = new MyDateChooser();
this.add(chooser);
…
private static class MyDateChooser extends JDateChooser {

    @Override
    public void actionPerformed(ActionEvent e) {
        …
        Date date = dateEditor.getDate();
        if (date == null) {
            Calendar c = Calendar.getInstance();
            c.add(Calendar.YEAR, -10);
            calendar.setTime(c.getTime());
        } else {
            calendar.setTime(date);
        }
        jcalendar.setCalendar(calendar);
        …
    }
}

image