The JMonthView component enables users to view and select dates by means of an intuitive calendar interface. For now, it is restricted to just one month but future versions will expand it to display more months.
It extends JMonth and the inherited methods affect all the months represented by this component. In addition, a JMonthView provides the following:
Two buttons at the top to scroll the month in view.
A status bar that contains a today button that selects the today date and a none button that un selects 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 on the top buttons to scroll the month in view.
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.
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 Java Date Picker components is included in the javadatepicker.jar jar file. The distribution also includes the javadatepicker-i18n.jar file which contains localizations of the monthview.properties file.
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 javadatepicker-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 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 setDisplayToday method. To find out whether it is displayed or not use isDisplayToday method.
The following line of code hides the today button:
monthView.setDisplayToday(false);