Sunday, November 20, 2011

004. JTEXTFIELD, JPASSWORDFIELD, JTEXTAREA


A jTextField is a basic text control that lets the user enter a small amount of text. A textfield fires an action event when the user pressing the ENTER.
The jPasswordField class, a subclass of jTextField, provides specialized text fields for password entry. For security reason, a passwordfield doesn't show the characters that the user types. A passwordfield store its value as an array of characters, rather than as string. A passwordfield fires an action event when the user pressing the ENTER.
The jTextArea class provides a component that displays multiple lines of text and optionally allows the user to edit the text.

Assume: we have understood about a blog archive 001. THE BEGINNING.
  1. In package01, create a new jFrame, named FrameText01.
     



  2. Add jPanel, set layout=null;






  3. Add 2 jTextFields;





  4. Right click jTextField1 select Edit Text, set empty.
  5. Right click jTextField1,select Change Variable Name ..., named tx1.
  6. Do the same to jTextField2,Change Variable Name = tx2, click OK. Set horizontalAlignment property to CENTER.



  7. Right click tx1, select Events - Action - actionPerformed. type following script:

    tx2.setText(tx1.getText());
    tx1.transferFocus();

    transferFocus
    methode for moving focus from tx1.


  8. Run that file particularly by press SHIFT+F6.
  9. Test it by type in tx1 then press ENTER, we will see the result in tx2.
  10. Close that running program.
  11. Add jPasswordField, Right click select Change Variable Name..., named pf.
    Press F2, then remove the text.
     


  12. Set pf position as following:


  13. Right click pf, select Events - Action - actionPerformed type following script:
    tx2.setText(String.valueOf(pf.getPassword()));
    tx2.requestFocus();



    requestFocus methode for moving focus to tx2.

  14. Run that file particularly by press SHIFT+F6.
  15. Type some characters in pf control then press ENTER, we will see the result in tx2.
  16. Close that running program.
  17. Add jTextArea, then right click select Change Variable Name..., named ta.



    Before giving a name for jTextArea, we have to sure that jTextArea is being selected, by noticing the  inspector tab.



  18. Set ta position as following.

     


  19. Run that file particularly by press SHIFT+F6.
  20. Type some characters in ta control then press ENTER, the cursor move to the next line.
    It is different from jTextField control, if we pressed ENTER. In jTextArea, We must press CTRL-TAB for moving cursor to another control.

  21. If we want to set right alignment property on jTextArea, we must add the following script :
    ta.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);


But, it would be better using jFormattedTextField if we would:
  • calculate the number that we 've entried;
  • limit the number of character that we 've entried;
  • limit the type of character (number or alphabet);
  • display a date format; or
  • accept some input with a certain format (mask).
Thank you, please try it. If I have a time, in the next blog we will discuss about JFormattedTextField.