javajdatechooser

How do i hide or disable all the Sundays on a JDateChooser?


I want to disable all the Sundays on the JDateChooser but I don't know how.

I saw some answers earlier while searching and they're using a range with start and end but in my case it's all the sundays in the jdatechooser. It was for our school project and we're not allowed to drag and drop controls so I declared the datechooser and imported the com.toedter.calendar.JDateChooser;

Below is my code for JDateChooser. I really hope to learn more, thank you.

    JDateChooser date = new JDateChooser(new Date());
                        date.setBounds(120,150,150,30);
                        sapp1.add(date);

Solution

  • As I mentioned in the comment to the original post, you can obtain JCalendar from JDateChooser and customize it to achieve the desired result.

    JDateChooser date = new JDateChooser(new Date());
    date.getJCalendar().getDayChooser().addDateEvaluator(new MyDateEvaluator());
    

    You can set a custom IDateEvaluator which allows to makes all Sundays disabled.

    @Override
    public boolean isInvalid(Date date) {
        return date.getDay() == 0;
    
    }
    

    Here is the code that makes all Sundays disabled:

    import com.toedter.calendar.IDateEvaluator;
    import com.toedter.calendar.JDateChooser;
    
    import javax.swing.*;
    import java.awt.*;
    import java.util.Date;
    
    public class CustomizedDateChooser {
        public static void main(String[] args) {
            JFrame f = new JFrame("ComboBox Example");
    
            JDateChooser date = new JDateChooser(new Date());
            date.getJCalendar().getDayChooser().addDateEvaluator(new MyDateEvaluator());
            date.setBounds(200,200,200,50);
            JPanel p = new JPanel();
            p.add(new JLabel("Choose a Date:"));
            p.add(date);
            f.add(p);
            f.setLayout(new FlowLayout());
            f.setSize(400, 500);
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            f.pack();
            f.setVisible(true);
        }
    
        private static class MyDateEvaluator implements IDateEvaluator {
    
            @Override
            public boolean isSpecial(Date date) {
                return false;
            }
    
            @Override
            public Color getSpecialForegroundColor() {
                return null;
            }
    
            @Override
            public Color getSpecialBackroundColor() {
                return null;
            }
    
            @Override
            public String getSpecialTooltip() {
                return null;
            }
    
            @Override
            public boolean isInvalid(Date date) {
                return date.getDay() == 0;
    
            }
    
            @Override
            public Color getInvalidForegroundColor() {
                return null;
            }
    
            @Override
            public Color getInvalidBackroundColor() {
                return null;
            }
    
            @Override
            public String getInvalidTooltip() {
                return null;
            }
        }
    
    }