Use swing as a calendar Control Program

Source: Internet
Author: User
Tags dateformat
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 table ).

Table

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.

Table 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 buttons are controlled 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.TableADescribes the Application Programming Interface named datebutton, andList BIs 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 (referList C), You can freely select the month, year, or day from the list (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.

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.