Search code examples
javaswingjcomboboxlocaldate

How can I make a calendar using JComboBox?


I want to make it adaptive, like if Feb got 29 days in 2000 but it will change into 28 when 2001.

I want to do it while using JComboBox

How can I make action calendar using a combo box?

    JComboBox jcb,jcb1,jcb2;    
    db(){
    JFrame jf = new JFrame("register");
    jf.setLayout=(new FlowLayout());
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    String aa1="0"+1+"-"+"0"+2+"-"+2000;
    date = LocalDate.parse(aa1,dtf);
    Integer day[] = new Integer[date.lengthOfMonth()];
    for(int i=0;i<date.lengthOfMonth();i++) {
        day[i]=i+1;
    }
    jcb = new JComboBox<>(day);
    Integer month[] = new Integer[12];
    for(int i=0;i<12;i++) {
        month[i]=i+1;
    }
    jcb1 = new JComboBox<>(month);

    Integer year[] = new Integer[80];
    for(int i=0;i<80;i++) {
        year[i]=i+1940;
    }
    jcb2 = new JComboBox<>(year);
    jf.add(jcb);
    jf.add(jcb1);
    jf.add(jcb2);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setBounds(300,300,300,300);
    jf.setVisible(true);
}

Solution

  • As @Andrew Thompson mentioned in comments, JComboBoxes for date selection is not a good idea. Take a look at Which one is the best Java datepicker.

    However, if you still insist of using comboboxes, in order to achieve what you want, you will have to add an ActionListener to month/year combobox in order to re-fix the model (items) of days combobox.

    Take a look at this example:

    public class Test extends JFrame implements ActionListener {
        private JComboBox<Integer> yearBox;
        private JComboBox<Integer> monthBox;
        private JComboBox<Integer> dayBox;
    
        public Test() {
            super("test");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            getContentPane().setLayout(new FlowLayout());
    
            yearBox = new JComboBox<>();
            for (int i = 1940; i <= LocalDateTime.now().getYear(); i++) {
                yearBox.addItem(i);
            }
            yearBox.addActionListener(this);
    
            monthBox = new JComboBox<>();
            for (int i = 1; i <= 12; i++) {
                monthBox.addItem(i);
            }
            monthBox.addActionListener(this);
    
            dayBox = new JComboBox<>();
    
            add(new JLabel("year:"));
            add(yearBox);
            add(new JLabel("month:"));
            add(monthBox);
            add(new JLabel("day:"));
            add(dayBox);
    
            //Start with current year selected
            yearBox.setSelectedIndex(yearBox.getItemCount() - 1);
    
            setSize(400, 400);
            setLocationRelativeTo(null);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            int year = (int) yearBox.getSelectedItem();
            int month = (int) monthBox.getSelectedItem();
            int daysInThisMonth = LocalDate.of(year, month, 1).lengthOfMonth();
            int previousSelection = dayBox.getSelectedItem() != null ? (int) dayBox.getSelectedItem() : 1;
            dayBox.removeAllItems();
            for (int i = 1; i <= daysInThisMonth; i++) {
                dayBox.addItem(i);
            }
            if (previousSelection >= dayBox.getItemCount())
                //select last index of month
                dayBox.setSelectedIndex(dayBox.getItemCount() - 1);
            else
                dayBox.setSelectedItem(previousSelection);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> new Test().setVisible(true));
        }
    
    }