/** Update the calendar panel to display the currently selected date. */ private void updateCalendarComponents() { if ((selectedComponents & DISPLAY_DATE) > 0) { // Unselect all visible dates offScreenButton.setSelected(true); // Get the display date. We only need the month and year displayMonth = displayCalendar.get(Calendar.MONTH); displayYear = displayCalendar.get(Calendar.YEAR); // Get the localized display month name and year String month = formatMonth.format(displayCalendar.getTime()); String year = Integer.toString(displayYear); { Object[] args = {month, year}; monthYearLabel.setText(MessageFormat.format(bundle.getString("MonthYearTitle"), args)); } // If the month or year have changed, we need to re-lay out // the days. Otherwise, we don't if (!month.equals(lastMonth) || !year.equals(lastYear)) { // Create a temporary calendar that we will use to // determine where day 1 goes and how many days there are // in this month Calendar temp = (Calendar) displayCalendar.clone(); temp.set(Calendar.DATE, 1); int dayOfWeek = temp.get(Calendar.DAY_OF_WEEK); int firstDay = temp.getFirstDayOfWeek(); // Determine how many blank slots occur before day 1 of this // month int dayPtr; for (dayPtr = 0; dayPtr < 7; dayPtr++) { int curDay = ((firstDay - 1) + dayPtr) % 7 + 1; if (curDay != dayOfWeek) { dayButtons[0][dayPtr].setText(""); dayButtons[0][dayPtr].setEnabled(false); } else { break; } } // Determine the number of days in this month int maxDays = temp.getActualMaximum(Calendar.DATE); // Fill in the days int row = 0; for (int day = 1; day <= maxDays; day++) { dayButtons[row][dayPtr].setText(Integer.toString(day)); dayButtons[row][dayPtr].setEnabled(true); // If this is the selected date, select the button; // otherwise, deselect it if (day == selectedDay && displayMonth == selectedMonth && displayYear == selectedYear) { dayButtons[row][dayPtr].setSelected(true); } else { dayButtons[row][dayPtr].getModel().setSelected(false); } // Wrap as needed dayPtr = (dayPtr + 1) % 7; if (dayPtr == 0) row++; } // Set the blanks slots after the last day while (row < 6) { dayButtons[row][dayPtr].setText(""); dayButtons[row][dayPtr].setEnabled(false); dayButtons[row][dayPtr].getModel().setSelected(false); dayPtr = (dayPtr + 1) % 7; if (dayPtr == 0) row++; } } } // Update the time component, if displayed if ((selectedComponents & DISPLAY_TIME) > 0) { // If no date is selected, we set the date used by the time // field to today @ noon. We also make the field insensitive // -- the user must pick a non-null date before being able to // change the time (unless all we have is a time field) if (isNullDate) { Calendar temp = (Calendar) selectedCalendar.clone(); temp.setTime(new Date()); temp.set(Calendar.HOUR, 12); temp.set(Calendar.MINUTE, 0); temp.set(Calendar.SECOND, 0); spinnerDateModel.setValue(temp.getTime()); spinner.setEnabled((selectedComponents & DISPLAY_DATE) == 0); } // If a date is selected, use it else { spinner.setEnabled(JCalendar.this.isEnabled()); spinnerDateModel.setValue(selectedCalendar.getTime()); spinnerDateModel.setStart(null); spinnerDateModel.setEnd(null); spinner.revalidate(); } } }