LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   java swing JTextArea (http://www.linuxquestions.org/questions/programming-9/java-swing-jtextarea-326160/)

gauravbagga 05-23-2005 04:50 AM

java swing JTextArea
 
I want to block numerical inputs in a JTextArea
i wrote a code for AWT TExtArea it works but for JText it doesn't
can any body tell me how to achieve same thing with JText
my code is as folows

public void keyPressed(KeyEvent ke){

if((ke.getKeyCode() >= 48 && ke.getKeyCode() <=57)){
ke.setKeyCode(3);
}
}

listener added to awt textarea

but same does not works with swings the character gets printed in textarea while in awt it blocks the input

any suggestions

mrcheeks 05-23-2005 05:34 AM

I would use a document listener or a custom swing document with the insertstring method. You can write a method isNumeric(String a) or use the method isNumeric(String str) from commons lang(http://jakarta.apache.org/commons/la...ringUtils.html)

Code:

...
JTextArea txt = new JTextArea();
txt.setDocument(new LettersDocument());
....
class LettersDocument extends PlainDocument{
  public void        insertString(int offs, String str, AttributeSet a) {
      if(!isNumeric(str){
          super.insertString(offs,str,a);
      }
}

}



All times are GMT -5. The time now is 02:17 AM.