Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut and Paste in JTextArea

I'm developing an application which requires that only 165 characters to be in the JTextArea. I've imposed that condition. I've used a static counter for counting number of characters entered in the textarea and also coded to handle the condition when user deletes any string from the text the counter must be incremented by considering length of the string selected.

However now I want to handle the condition when user performs 'cut' or 'paste' option by pressing 'Ctrl+X' and 'Ctrl+V'. I know that default methods from JTextComponent are inherited in JTextArea but I want to get the cut text and know the length of the cut text so as to decrement the counter maintained for characters and increment it while pasting by appropriate amount.

like image 330
Supereme Avatar asked Dec 30 '22 03:12

Supereme


1 Answers

Create a DocumentFilter and set it on a new PlainDocument. Use this document to create the JTextArea. (Or use the default document of the JTextArea, after casting to AbstractDocument).

See my answer here for a sample.

like image 126
user85421 Avatar answered Jan 15 '23 10:01

user85421