JFormattedTextField Components and Format Input

zhaozj2021-02-11  187

Learning to achieve the purpose of formatting text input with the least amount of work: John Zukowski Translation: Sean2002.6.

Using input verification and focus monitoring reception formatting input is not difficult. In this article, John showed you how to use the JFormattedTextField component prompt to enter numbers, dates, and formatted input information.

Inside the J2SE 1.4, two new Swing components are added: JSPinner and JFormattedTextField. We have already introduced Jspinner components in Merlin's magic columns, and now let's take a look at JFormattedTextField.

Although the appearance of the JFormattedTextField component is the same as JtextField looks, their behavior is completely different. In the simplest case, you can give an input mask of a phone number "(###) ### - ####", JFormattedTextField does not accept any input that does not conform to such format. The case of complex points is there in a display format and an input format. For example, the default date input format allows valid month or date editing to scroll at the cursor position.

The input one of JFormattedTextField can receive is explicitly defined by Mask, and two are specified by a value of the component. Based on the latter case, the component uses the Factory design mode to find the default Formatter for this specified value. The DEFAULTFORMATTERFACTORY component produces a built-in date format, digital format, Java.Text.Format subclass, and any inclusive formatter.

Let's take a look at how to let this component work.

Configuring that the received input is most often, we use a MaskFormatter instance to implement mask input. The Maskformatter class is in a javax.swing.text package, specifying the entered range with a series of letters. This series of eight characters each represents a character that is entered, as the list below:

# A number? One letter A A number or letters * any character u One letter, lowercase letters are converted to the corresponding uppercase letter L A letter, uppercase letters are converted to the corresponding kid letter h A hexadecimal number (AF, AF, 0-9) 'escape characters

In addition to Maskformatter, you can also use the DateFormat and NumberFormat classes to specify the input format. The table below shows some possible formats. // Four-Digit Year, FOLOWED by Month name and day of month, // each sepaper and day tasts (-) DateFormat Format = New SimpleDateFormat ("YYYY - MMMM - DD"); DateFormatter DF = New DateFormatter Format); // US Social Security Numbermaskformatter Mf1 = New Maskformatter ("### - ## - ####"); // US Telephone Numbermaskformatter MF2 = New Maskformatter ("(###) ### - # ### "); Once you have determined the input format, you can pass the formatter to the JFormattedTextField class, as shown: jformattedTextField FTF1 = New JFORMATTEXTFIELD (DF); in addition to the formatter used, there are other configurations selected. For example, using MaskFormatter, you can set up a PlaceHolder character with the SetPlaceHolderCharacter (CHAR) method. Similarly, if the date is input box, it can initialize a value for your input box, so that the user knows the entered format. Summary All Everything is just an input box for a mask. The following program gives you a complete example, which checks the new formatting input capability by combining the previous code snippet. The picture below shows the appearance. Arbitrary Adjust Formatter to detect other input masks.

转载请注明原文地址:https://www.9cbs.com/read-5773.html

New Post(0)