Hide or disable past dates on JDateChooser

user3337385 picture user3337385 · Feb 28, 2014 · Viewed 13k times · Source

I want to disable or hide or make the past dates in JDateChooser not selectable. How can I make this? I've tried to use .setSelectableDateRange but it doesn't work. I also tried .setMinSelectableDate() but still no luck. I don't know but netbeans doesn't seem to know those because those doesn't show up in code suggestions. I'm using it like this:

public void dateset() {
    jDateChooser1.getCalendar(). //What to put here? It doesn't have .setSelectableRange
}

I only tried the one that I've found on this one: How to show only date after the date of today in JCalendar

I think that post was already outdated. Please help.

Answer

dic19 picture dic19 · Mar 1, 2014

Here:

jDateChooser1.getCalendar().

You're trying to set date's boundaries to a java.util.Calendar object which is not possible. Maybe you're confused with getJCalendar() which returns a JCalendar object:

jDateChooser1.getJCalendar().setMinSelectableDate(new Date()); // sets today as minimum selectable date

Note you can set minimum selectable date directly on date chooser:

jDateChooser1.setMinSelectableDate(new Date()); // sets today as minimum selectable date

Inspecting JDateChooser source code you can see this method is just forwarded to the JCalendar object:

public class JDateChooser extends JPanel implements ActionListener,
        PropertyChangeListener {

    protected IDateEditor dateEditor;
    protected JCalendar jcalendar;

    ...

    public void setMinSelectableDate(Date min) {
        jcalendar.setMinSelectableDate(min);
        dateEditor.setMinSelectableDate(min);
    }

    ...
}

You may also want to take a look to How to disable or highlight the dates in java calendar for a better understanding on IDateEvaluator interface which is actually the key on this whole date validation matter.