JFormattedTextField provide a way to specify the valid set of characters that can be typed in aJTextField.
Assume: we have understood about a blog archive 001. THE BEGINNING.
- In package01, create a new jFrame, named FrameFormattedText01.
- Add jPanel, set layout=null;
- Add 8 jLabel and 8 jFormattedTextField controls, like as following picture:
- Change their properties as table below:
Controls Property Value jLabel1 text just letter (mask) jLabel2 text just number (mask) jLabel3 text with a format (mask) jLabel4 text date jLabel5 text number with decimal jLabel6 text percent jLabel7 text number jLabel8 text result jFormattedTextField1
name
(right click - Change Variabel Name ...)ft1 jFormattedTextField2 name ft2 jFormattedTextField3 name ft3 jFormattedTextField4 name ft4 jFormattedTextField5 name ft5 jFormattedTextField6 name ft6 jFormattedTextField7 name ft7 jFormattedTextField8 name ft8
- Select all jLabel, set horizontalAlignment property = RIGHT.
- Select all jFormattedTextField, then remove text property.
- Click ft1, set formatterFactory property, Category= Mask, Format= ?????, click OK.
- Click ft2, set formatterFactory property, Category= Mask, Format= #####, click OK.
- Click ft3, set formatterFactory property, Category= Mask, Format= (****) *****, click OK.
- Click ft4, set formatterFactory property, Category= Date, Format= dd-MM-yyyy, click OK.
- Click ft5, set formatterFactory property, Category= Number, Format= #,##0.00, click OK.
- Click ft6, set formatterFactory property, Category= Percent, Format= #0.00%, click OK.
- Click ft7, set formatterFactory property, Category= Number, Format= #,##0.###, click OK.
- Click ft8, set formatterFactory property, Category= Number, Format= #,##0.###, click OK.
- Add jButton, named btsum, set Text property = calculate.
- Right click btsum, select Events - actionPerformed
- Add script:
Float percent=Float.valueOf(ft6.getValue().toString());
Float m=Float.valueOf(ft7.getValue().toString());
ft8.setValue((percent * m) + m);
- Run this jFrame by press SHIFT+F6
- We will see:
- ft1 just accepted the letter (limited)
- ft2 just accepted the number (limited)
- ft3 accepted any character (limited)
- ft4 accepted date, and returning a datetype
- ft8 display the result of calculation, when we clicked the button.
Note:
To get a zero-length string from a blank mask-jFormattedTextField, we have to set trim() function. Example:
String zerolength = ft1.getText().trim();
These are the characters that you can use in the formatting mask:
Character | Description |
# | Any valid number (Character.isDigit) |
' | Escape character, used to escape any of the special formatting characters. |
U | Any character (Character.isLetter). All lowercase letters ar mapped to uppercase |
L | Any character(Character.isLetter). All uppercase letters ar mapped to lowercase |
A | Any character or number (Character.isLetter or Character.isDigit) |
? | Any character (Character.isLetter) |
* | Anything |
H | Any hex character (0-9, a-f, A-F) |
Note:
Be careful if we will rename the class/file that contains a mask-JFormattedTextField. The error-message will be displayed when we run the program. We have to reset a formatterFactory property.