Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextArea

 
Hasnain Javed
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
How can I restrict JTextArea to allow only 255 Words. Any Suggestions?
Regards.
Hasnain.
[ August 16, 2005: Message edited by: Hasnain Javed ]
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you can take the input one by one remove the spaces and add it to an array of strings of length 255 then print it on the textpane
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you can even count the spaces and when the counter reach 225 disable writing
 
Hasnain Javed
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply but how do I do it? Can I register an ActionEvent object to check every time when a letter is entered?
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you can check if keypressed is the space key and each time increase the counter
you can do that by registering to keypressed listener
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But one could press backspace and erase few lines..So i dont understand how u can accomplish this using keypressed listener..
I think there should be a property for textArea..not sure though!

check out this link..
http://java.sun.com/docs/books/tutorial/uiswing/components/textarea.html

Sorry if I have increased the confusion !!
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okey then he can count the spaces each time after key pressed so even if the user pressed backspace the counter will decrease
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a new object class that implements JTextArea. Within the new class, create a getWordCount() method that returns how many words have been entered. To add a word count restriction, add a maxWordCount attribute that can be set by the user. Whenever something is typed into the object, check that getWordCount() <= maxWordCount before adding.
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's more detail. It works, but there are a few bugs. But I think it's enough to get you going.

Main.java:


CustomJTextArea.java:


Hope this helps a bit.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would actually recommend doing one of the following:

Option One: Extend PlainDocument so that it ignores attempts to insert new words when there are enough already.

Option Two: Use JTextArea.setInputVerfier() so that users will be able to type more than the legal number of word, but the text area will not release focus until the number of words decreases again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic