dcsimg
Text Field
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ramanujam_ragavan
Posted On:   Thursday, September 5, 2002 05:28 AM

I need a Text field with the following features. 1. It must accept only alphabets CAPS and numbers 2. It must support only upto 3 characters I tried the following but in vain.. Pls.. help final JTextField editor = (JTextField) ((BasicComboBoxEditor) getEditor()).getEditorComponent(); editor.setDocument(new UpperCaseOnlyAllowed()); /** * Attaching anonymous inner class to ComboEditor in order to handle Key Events */ editor.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent ke) { //step 1 int kc = ke.getKeyCode();    More>>

I need a Text field with the following features.


  • 1. It must accept only alphabets CAPS and numbers
  • 2. It must support only upto 3 characters


    I tried the following but in vain.. Pls.. help



    final JTextField editor =
    (JTextField) ((BasicComboBoxEditor) getEditor()).getEditorComponent();

    editor.setDocument(new UpperCaseOnlyAllowed());


    /**

    * Attaching anonymous inner class to ComboEditor
    in order to handle Key Events

    */

    editor.addKeyListener(new KeyAdapter()

    {



    public void keyTyped(KeyEvent ke)

    {

    //step 1

    int kc = ke.getKeyCode();

    System.out.println(kc);

    boolean isKeyInValidRange = Character.isLetterOrDigit(ke.getKeyChar());

    System.out.println(isKeyInValidRange);


    if(kc == KeyEvent.VK_BACK_SPACE || kc == KeyEvent.VK_SHIFT)

    {

    isKeyInValidRange = false;


    }

    //step 3

    if (isKeyInValidRange)

    {

    System.out.println("Key is in Range");


    String text = editor.getText();

    String newText = "";
    int length = text.length();

    System.out.println("len = "+length);


    if(length < 3)

    {

    System.out.println("length is less than 3");

    editor.setText(editor.getText());

    }

    else if(length >= 3)

    {

    System.out.println("You have crossed the limits Buddy");

    newText = text.substring(0,2);

    editor.setText(newText);

    text = null;

    }


    }


    }

    });




    But when I tried running the code, it accepts the caps only filter, but accepts special chars as well.. Also, if I type beyond the third character, it replaces the last character with the typed one.... Some Code Example Please..


    Thanx in advance

  •    <<Less

    Re: Text Field

    Posted By:   Dmytro_Vorobyov  
    Posted On:   Monday, September 9, 2002 12:15 AM

    It's not right way.
    Read tutorial.You can find answer at it.


    You have to do it in such way


    a)
    editor.setDocument(new FieldDocumet());


    b) or create your own component extended JtextField with
    field picture (I did it)


    and control inserted symbols by your class FieldDocument




    protected class FieldDocument extends PlainDocument {

    public void insertString(int offs, String str, AttributeSet a)
    throws BadLocationException {

    if (str == null) {
    return;
    }
    if (str.length() + getLength() > len) {
    if (getLength() >= len) {
    return;
    }
    str = str.substring(0, len - getLength() - 1);
    }
    StringBuffer strInsert = new StringBuffer(str);

    if (picture != null) {
    for (int i = 0; i < strInsert.length(); i++) {
    if (picture.charAt(offs + i) == '9' &&
    !(Character.isDigit(strInsert.charAt(i)) ||
    strInsert.charAt(i) == ' ' || strInsert.charAt(i) == '+' || strInsert.charAt(i) == '-' || strInsert.charAt(i) == '.'
    || strInsert.charAt(i) == ','
    )
    ) {
    // Öèôðû Digital

    AWTUtil.beep();
    strInsert.deleteCharAt(i);
    }
    if (picture.charAt(offs + i) == '!') {
    // Âåðõíèé ðåãèñòð upper case

    strInsert.setCharAt(i, Character.toUpperCase(strInsert.charAt(i)));
    }
    }

    }
    if (strInsert.length() > 0) {
    super.insertString(offs, strInsert.toString(), a);
    }
    }
    }







    It work as inner class at such class as

    public class FieldGet extends JTextField implements FieldRead {
    ...............
    String picture = null;
    private int len = 10;
    ..................



    About | Sitemap | Contact