Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-23-2005, 03:50 AM   #1
LQ Newbie
Registered: Mar 2005
Posts: 7

Rep: Reputation: 0
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
Old 05-23-2005, 04:34 AM   #2
Senior Member
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 51
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) {



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
a java-swing problem... shosh Programming 2 06-26-2009 05:38 AM
JTextArea Question for Java Programmers Diederick Programming 1 09-16-2005 08:25 PM
Java JTextArea - limiting amount of characters entered Andy@DP Programming 3 03-10-2004 01:16 AM
Java help! Using Swing and Layouts Mega Man X Programming 7 02-05-2004 05:02 PM
missing java swing libraries? a1ghagh05t Programming 5 01-26-2004 06:04 AM > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration