1

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mfrancisp
  • 119
  • 1
  • 2
  • 12

2 Answers2

4

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

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Hi trash! I cant try it yet but from the looks of the code and ss above, you have two separate components. Mine only has the jdatechooser with a button for a popup jcalendar. What i cant seem to figure out is how to set a date to the popup jcalendar with the textfield being null. Im trying to avoid setting the date to the jchooser and instead set it as a default in the popup. Does my question make sense? – mfrancisp Mar 27 '15 at 17:51
  • Ah, I see; I updated the example to show the single component with popup editor; I don't see a way to make the field blank but give the popup a default value. – trashgod Mar 27 '15 at 20:21
  • Is the most recent addendum what you were looking for? – trashgod Mar 31 '15 at 01:26
  • Hi Trash, thats what i was looking for. However may have missed something when i tried your solution. I think for this to work, i would also need the whole code to make the popup appear right? – mfrancisp Mar 31 '15 at 01:32
  • Right; I didn't want to mix licenses; I included one line before and after the change for reference. – trashgod Mar 31 '15 at 10:53
0

Maybe there is another way of doing this, but this is what I have:

Date date = new Date();
Calendar calendar = Calendar.getInstance();
int subtractYearValue = 10;

int currentYear = Calendar.getInstance().get(Calendar.YEAR);
int currentMonth = Calendar.getInstance().get(Calendar.MONTH);
int currentDate= Calendar.getInstance().get(Calendar.DATE);

calendar.set(currentYear - subtractYearValue , currentMonth , currentDate);
date.setTime(calendar.getTimeInMillis());

dateChooser.setDate(date);
Cristian Marian
  • 750
  • 1
  • 8
  • 18
  • I was looking for a way to bypass putting the date value inside the textfield component of the jdatechooser in order for the jcalendar to set it's date to that value. By default i think the jcalendar sets it's date to current if texfield is null. – mfrancisp Mar 27 '15 at 14:51
  • Well, I dont think that is possible. The JCalendar sets it's date to current date when the texfield component is empty or when the value inside of it, cant be translated into an actual date. – Cristian Marian Mar 27 '15 at 15:01
  • @CristianMarian: Use a sinlge instance of `Calendar`; I've proposed an approach [here](http://stackoverflow.com/a/29305584/230513). – trashgod Mar 28 '15 at 14:21