LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-23-2005, 03:50 AM   #1
gauravbagga
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)){
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
 
Old 05-23-2005, 04:34 AM   #2
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 52
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);
       }
}

}
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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