Use swing as a Calendar control program

Source: Internet
Author: User
Tags abstract date dateformat interface modify set time
Program | control | Calendar dates are one of the most basic forms of data, so many languages or databases provide support for dates, or libraries that are built into or provided with this type. Java is no exception; the Java application programming interface provides a way to describe, compute, contrast, display, and analyze dates in a cross-platform, stand-alone language. In this article, I'm going to talk about the custom control of swing, which allows you to visually acquire time information instead of knocking on commands. In this way, you can avoid invalid time information, so that your users enjoy the visual way. Date
in Java

Java describes the date and time methods using the traditional date command or some of the Java libraries. Regardless of its name, the class of dates does not have the concept of a date in the ordinary sense (day, month, year, etc.) technically, it is only a result of the accumulation of one out of 10,000 seconds from the start of the January 1, 1970 00:00:00 GMT. Although these date-related classes have various methods of dealing with dates, this is not enough.





to facilitate the resolution of date problems, JDK 1.1 introduces an abstract class Java.util.Calendar. A subclass of the exact date of calculation uses a pointer to a specific date based on the calendar system. This is because the calendar system has always been a number of ways to calculate their own dates (such as the Gregorian calendar, Roman, lunar calendar).




The
Java application programming interface provides classes called Java.util.GregorianCalendar to process date information based on the solar system.





Finally, a class called Java.text.DateFormat is an abstract processing format that interprets date classes in a way that is independent of the language. This way depends on the conventions, the rules, and the location of known language requirements. A specific subclass called DateFormat does the actual work. For example, a subclass named Java.text.SimpleDateFormat allows you to analyze or set a date format based on an existing pattern.





Overall, these three most basic classes (date, Calendar, and DateFormat) provide a powerful and flexible system for dealing with dates. It may seem a bit confusing at first, but once you understand it, you'll see that the structure is very meaningful and has a strong logical background behind it. (Look at the JDK API documentation and you'll get a lot of documents and examples)

Date control of
custom


Although Java provides a way for you to manipulate date data, it may seem a bit cumbersome to let you handle three different classes. Sometimes you need to use a lot of methods to accomplish a simple task with many entities. In addition to those, swing does not provide an entry date component. Of course, if the use of standard control such as text box is not difficult to complete. However, dates are so common in applications that we often write the same code, input, analyze, and check time over and over again.





based on the above considerations, I have built a simple custom control that you can use to access date data. Basically, this is a standard button to display the date (see form a).





form A











Custom Date Control





When you click, a date dialog box pops up and allows you to select a new date (see Form B).





Click the OK key to complete an update.





form B








Calendar dialog box pops up


This simplifies the application logic because the consumer does not access an invalid date. And because the dialog box looks like a calendar, it's better for the user to master the very useful date information of the week. The custom key control is described in the class named Datebutton (List a). It's very concise to use-just enumerate an entity named Datebutton and add it to a box like a normal control load.




The
key is the entity that represents the date, and executes like a reflection of the "date" command. This is an accident. You can set and get time as long as the time registers named PropertyChangeListener change. Table A illustrates the application programming interface named Datebutton, and List B is an example of use.





form A





The custom key application programming interface is very simple. You can use one of these constructors to create an example and get or set time information correctly. Note that this is a standard keystroke, so it inherits many of the standard keystrokes, including how to register.





Method





Description





Datebutton (Date)





creates a new class called Datebutton to initialize the date.





Datebutton ()





creates a new class called 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 create a custom dialog box named Calendarchooser (refer to List C), where you can freely select the month, year, or day from the list (refer to table B). Although it is automatically displayed according to the name Datebutton control, you can use it in your own way. Create a class named Calendarchooser, and run it in a chosen way (refer to table B to complete the application programming interface).





form B





using the Calendar dialog box to select a date is very simple. Using a widget to build a dialog box entity, use one of these methods to select a date.





Method





Description





Datechooser (Dialog, title)





creates a new class named Datechooser, and gives its title and dialog information.





Datechooser (Dialog)





creates a new class called Datechooser and gives it dialog information.





Datechooser (Frame, title)





creates a new class called Datechooser and gives it framework and dialog information.





Datechooser (Frame)





creates a new class named Datechooser, and gives it a framework.





Select (Date)





displays a dialog box with the selected date, you can change the date arbitrarily, and return to the selected date. If you cancel the dialog box, return a null value and close the dialog box.





Select ()





the same as above, replaces the selected date with the system date.





Calendar dialog box Application programming interface





This class, called Datechooser, is somewhat complicated. I had to set up a new controller for every month's calendar because there was nothing in the standard control that I needed. On the other hand, it also shows how easy it is to love a new control, which can be accomplished by interacting with the mouse and the keyboard. Note that the display of the year in the year selection list is predetermined by a constant named First_year and Last_year. Similarly, other constants also determine the effect of some dialog box visual aspects, such as color. With a little design, it's not difficult to modify these parameters freely. Similarly, it is important to note that classes named Datechooser and classes named Datebutton are defined in English (month and week) or American date formats. Since these are also defined by constants, they are also very simple to modify. If you want to change it, it's completely language-neutral.








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.