The JMonthView component enables users to view and select dates by means of an intuitive calendar interface. It can display one or more months.
It extends JMonth and the inherited methods affect all the months represented by this component. In addition, a JMonthView provides the following:
Methods to specify how many months are displayed (rows, columns)
Two buttons at the top to scroll the month in view
A scrolling property that is used to determine how many months are scrolled from view
A status bar that contains a today button that selects the today date and a none button that unselects all selected dates
This section uses the application shown below to explore all these capabilities.
Try this:
Run MonthViewDemo using Java Web Start or consult the source code for yourself.
Click the Rows and Columns combo boxes to change the grid configuration.
Click on the top buttons to scroll the month in view.
Click on the Scrolling Delta combo box to specify the number of months to be scrolled by the top buttons.
Click on the today button to select the today date.
Click the Show Status Bar check box to hide the calendar status bar.
Click the Locale combo box and select German. The names of the week days and the names of the months are changed. Also the texts displayed on the Today and None buttons are changed.
Click the Locale combo box and select Czech. Notice that the texts displayed on the Today and None buttons appear in English because the component hasn't been localized for this language. We discuss below how to do that.
JMonthView can be configured to display one or more months. The months are arranged on a grid with a specified number of rows and columns.
By default, the component displays only one month. To change the number of rows and columns used to display the months, use the setRows and setColumns methods. The following lines of code configure the calendar to display two months horizontally:
monthView.setColumns(2); monthView.setRows(1);
One important feature of the JMonthView is that you can localize it for various languages and regions. The names of the week days, the first day of week, the names of the months and even the way the today date is displayed on the today label are determined on locale basis.
By default, the system's locale is used but it can be configured to use any locale. To change the locale, use the setLocale method.
The following line of code changes the locale to Spanish:
monthView.setLocale(new Locale("es", "ES"));By default, the texts displayed on the "Today" and "None" labels are configured from the monthview.properties file which is placed in com.standbysoft.component.date.swing package. The reason for this is that we can determine the names of week days and months from the system but we cannot do that for strings like "Today" or "None".
The following code shows the syntax for this file:
Calendar.todayLabel=Today: {0,date,short}
Calendar.noneLabel=NoneThis file, along with other code that is needed to use the JDatePicker suite components is included in the jdatepicker.jar jar file. The distribution also includes the jdatepicker-i18n.jar file which contains localizations of the monthview.properties file. The component is now localized for the following languages: Danish, Dutch, English, Finish, French, German, Hungarian, Italian, Norwegian, Polish, Portuguese, Romanian, Spanish and Swedish.
For instance, the Spanish localization is:
Calendar.todayLabel=Hoy: {0,date,short}
Calendar.noneLabel=NingunoIf you find there is not an appropriate monthview.properties file for your locale or want to change an existing one, just alter the jdatepicker-i18n.jar file. In this way, all changes are isolated in one place.
The setEmptySelectionAllowed method is inherited from JDateComponent and we re discuss it here because it controls whether the None button is displayed or not.
When empty selection is allowed, the None button is displayed in the bottom-right corner. When clicked, no date will be selected anymore. To clear all selected dates, one can also use the DELETE or BACKSPACE keys.
The default implementation of JMonthView has two top buttons that scroll the month(s) in view. When the calendar has only one month, it is natural to scroll to the next or previous month when the buttons are clicked. But when there are displayed three months, for instance, should it scroll one month or three months?
To support this decision, the component provides the scrollingDelta property which is used to indicate the number of months that are scrolled when a scrolling button is clicked. The following lines of code show how to configure a three months calendar to scroll three months:
monthView.setRows(1); monthView.setColumns(3); monthView.setScrollingDelta(3);
The today button is meant to help users locate the today date faster. Left-clicking on it selects the today date. By default, the today button is visible. The DateFormat.SHORT format and the current locale are used to format the current date displayed by the button.
You can show or hide it using the setTodayButtonVisible method. To find out whether it is displayed or not use isTodayButtonVisible method.
The following line of code hides the today button:
monthView.setTodayButtonVisible(false);