I am try to populate a JComboBox with the days of a particular month and year. I researched the problem and came up with this:

        public void actionPerformed(ActionEvent e)
        {
            //code to respond to the buttons goes here
            if ( e.getSource() == cmbMonth || e.getSource() == cmbYear ) {
                cmbDay.removeAllItems();
                int year = Integer.parseInt(cmbYear.getSelectedItem().toString());
                int month = cmbMonth.getSelectedIndex() + 1;
                Calendar currMonYear = Calendar.getInstance();
                currMonYear.set(Calendar.YEAR, year);
                currMonYear.set(Calendar.MONTH, month);

                int daysInMon = currMonYear.getActualMaximum(Calendar.DAY_OF_MONTH);

                for ( int i = 1; i <= daysInMon; ++i )
                    cmbDay.addItem(i);
                int x = 0;
            }

        }//end actionPerformed()

Though I am getting incorrect values. Any suggestions?

saying what the problem is might be a good start.

The Calendar.set() methods that I used don't seem to be setting the month and year of the Calendar object correctly.

I checked the cdate property of the Calendar in debug mode and it had stored the year as 2013 and month as 6 even though the approiate date would be 3/2012. Not sure if the cdate is the property to check...

I scraped the Calendar idea and tried to use Joda Time and figured it out:

        public void actionPerformed(ActionEvent e)
        {
            //code to respond to the buttons goes here
            if ( e.getSource() == cmbMonth || e.getSource() == cmbYear ) {
                cmbDay.removeAllItems();
                int year = Integer.parseInt(cmbYear.getSelectedItem().toString());
                int month = cmbMonth.getSelectedIndex() + 1;;
                DateTime currMonYear = new DateTime(year, month, 1, 12, 0, 0, 000);

                int daysInMon = currMonYear.dayOfMonth().getMaximumValue();
                for ( int i = 1; i <= daysInMon; ++i )
                    cmbDay.addItem(i);
                int x = 0;
            }

        }//end actionPerformed()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.