Use Java Swing as a calendar Control Program

Source: Internet
Author: User

Date is one of the most basic data forms, so many languages or databases provide support for date, or built-in or provide this type of database. Java is no exception. Java's application programming interface provides the description, calculation, comparison, display, and portable analysis date in a cross-platform independent language. In this article, I want to talk about the custom control of Swing, which allows you to obtain time information visually rather than using command-based methods. In this way, you can avoid invalid time information and enjoy it visually.

Date in Java

Java describes the Date and time using the traditional Date command or some Java sub-libraries. No matter what its name is, the date class does not have the concept of date in the general sense (day, month, year, etc.) Technically speaking, it is only a result of accumulating one second from 00:00:00 GMT on January 1, January 1, 1970. Although these date classes have various date processing methods, this is not enough.

JDK 1.1 introduces an abstract java. util. Calendar class to easily solve the date problem. The subclass of a specific calculation date uses a pointer to a specific date based on the calendar system. This is because the calendar system has always been used to calculate its own date (such as the Gregorian calendar, the Roman calendar, and the lunar calendar ).

The Java application programming interface provides a class named java. util. GregorianCalendar to process date information based on the calendar system.

In the end, the class named java. text. DateFormat is an abstract processing format that interprets the date type independently of the language. This method relies on those conventions, rules, and locations where known language requirements are required. The specific subclass named DateFormat performs actual work. For example, a subclass named java. text. SimpleDateFormat allows you to analyze or set a date format based on the existing method.

In general, these three basic classes (Date, Calendar, and DateFormat) provide a powerful and flexible Date processing system. It may seem a bit confusing at first, but once you understand it, you will see that this structure is very meaningful and has a strong logical background behind it. (If you look at the jdk api documentation, you will get more documents and examples)

Customized date Control

Although Java provides methods for you to operate on date data, it may seem a little troublesome if you want to process three different classes. Sometimes you need to use many methods to complete a simple task using many entities. Except those, Swing cannot provide components for the date of entry. Of course, it is not difficult to use standard controls such as text box. However, date is so common in applications that we often write the same code, input, analysis, and check time over and over again.

Based on the above considerations, I have established a simple custom control that you can use to access date data. Basically, this is A standard button for displaying dates (see figure ).

Figure

Custom date Control

After you click, a date dialog box is displayed, allowing you to select a new date (see Table B ).

Click OK to complete an update.

Figure B

   Calendar dialog box

This simplifies the application logic because the user does not access an invalid date. In addition, since the dialog box looks like a calendar, this is more conducive to the user's understanding of the day of the week this very useful date information.

The Custom button control is described in the class named DateButton (List. Easy to use-just list an object named DateButton and add it to a box just like loading a normal control.

The buttons are the objects of the date. They are executed as if they were reflected by the "date" command. In this case, you can set and get the time as long as the time register named PropertyChangeListener changes. Table A illustrates the application programming interface named DateButton, and list B is an example.

Table

The Custom button Application Programming Interface is very simple. You can use a constructor to create an example and get or set the time information correctly. It should be noted that this is a standard button, and therefore it inherits many standard button methods, including the registration method.
Method Description
DateButton (Date) Create a new class named DateButton to initialize the date.
DateButton () Create a new class named DateButton to initialize the system date.
GetDate () Get the correct date value.
SetDate (Date) Set the correct date value.

   Application Programming Interface for custom Key Control

Calendar dialog box

In addition to customizing a controller named DateButton, we also need to create a custom dialog box named CalendarChooser (refer to list C). You can freely select a month from the list, year or day (see Table B ). Although it is automatically displayed according to the control named Datebutton, you can use it in your own way. Create a class named CalendarChooser and run it in an optional way (refer to table B to complete the application programming interface ).

Table B

Using the calendar dialog box to select a date is very simple. Use a widget to create an object in a dialog box, and use one of the methods to select a date.
Method Description
DateChooser (Dialog, title) Create a new class named DateChooser and assign its title and dialog information.
DateChooser (Dialog) Create a new class named DateChooser and give it dialog information.
DateChooser (Frame, title) Create a new class named DateChooser and grant its framework and dialog information.
DateChooser (Frame) Create a new class named DateChooser and assign it to the framework.
Select (Date) The displayed dialog box shows the selected date. You can change the date at will. The selected date is returned. If you cancel the dialog box, a null value is returned and the dialog box is closed.
Select () Similarly, use the system date instead of the selected date.

   Calendar Dialog Box Application Programming Interface

This class named DateChooser is somewhat complicated. I have to create a new controller for the calendar of each month because these standard controls do not have what I need. On the other hand, this also shows how easy it is to develop a new control, which can be accomplished by interacting with the mouse and keyboard.

Note that the year display in the year selection list is pre-determined by the constants FIRST_YEAR and LAST_YEAR. Similarly, other constants also determine the visual effects of some dialogs (such as colors. It is not difficult to modify these parameters with a little design.

Likewise, it is very important that both the class DateChooser and the class DateButton are defined in the English (month and day of the week) or American date format. Since these are also defined by constants, the modification is also very simple. If you want to modify it, it is completely irrelevant to the language.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.