Friday, November 25, 2011

005. JFORMATTEDTEXTFIELD



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.
  1. In package01, create a new jFrame, named FrameFormattedText01.


  2. Add jPanel, set layout=null;

  3. Add 8 jLabel and 8 jFormattedTextField controls, like as following picture:




  4. 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

  5. Select all jLabel, set horizontalAlignment property = RIGHT.
  6. Select all jFormattedTextField, then remove text property.
  7. Click ft1, set formatterFactory property, Category= Mask, Format= ?????, click OK.




  8. Click ft2, set formatterFactory property, Category= Mask, Format= #####, click OK.
  9. Click ft3, set formatterFactory property, Category= Mask, Format= (****) *****, click OK.
  10. Click ft4, set formatterFactory property, Category= Date, Format= dd-MM-yyyy, click OK.
  11. Click ft5, set formatterFactory property, Category= Number, Format= #,##0.00, click OK.
  12. Click ft6, set formatterFactory property, Category= Percent, Format= #0.00%, click OK.
  13. Click ft7, set formatterFactory property, Category= Number, Format= #,##0.###, click OK.
  14. Click ft8, set formatterFactory property, Category= Number, Format= #,##0.###, click OK.
  15. Add jButton, named btsum, set Text property = calculate.


  16. Right click btsum, select Events - actionPerformed
  17. Add script:
    Float percent=Float.valueOf(ft6.getValue().toString());
    Float m=Float.valueOf(ft7.getValue().toString());
    ft8.setValue((percent * m) + m);



  18. Run this jFrame by press SHIFT+F6
  19. 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.