-   Programming (
-   -   java swing JTextArea (

gauravbagga 05-23-2005 03: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)){

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 04: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(


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


All times are GMT -5. The time now is 11:11 PM.