Question

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.

Était-ce utile?

La solution

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.

Autres conseils

Try this example..

package chooseyourdate;

import com.toedter.calendar.JCalendar;
import com.toedter.calendar.JDateChooser;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFrame;

public class MainFrame extends JFrame {

    private JDateChooser chooser;

    public MainFrame() {
        JCalendar calendar = new JCalendar(GregorianCalendar.getInstance());
        chooser = new JDateChooser(calendar, new Date(), "dd.MM.yy", null);
        GregorianCalendar cal = (GregorianCalendar)GregorianCalendar.getInstance();
        // set the max date
        cal.set(2015, 10, 10);
        // MinDate is the current Date
        // MaxDate you can set in the GregorianCalendar object
        chooser.setSelectableDateRange(new Date(), cal.getTime());
        chooser.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                // to something...
            }
        });
        this.setSize(new Dimension(800, 600));

        this.getContentPane().add(chooser, BorderLayout.NORTH);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top