Jump to content
xisto Community
Sign in to follow this  
NewGuyinTown

Few Questions About Jtextarea

Recommended Posts

There are a few things I want to ask:1.) I want to know if there is a way to stop the JTextArea from expanding as the user types text into it.I want the TextArea's width locked.2.) I want to get each line (or row) of text the user typed in after the sumbit button is hit separately from the whole string of the TextArea. I have not tested out the getText() method, but I'm pretty sure it will return a string of the whole TextArea.3.) Set a maximum characters per line (4)Thank you,Simon Tang.

Edited by miCRoSCoPiC^eaRthLinG (see edit history)

Share this post


Link to post
Share on other sites

There are a few things I want to ask:1.) I want to know if there is a way to stop the JTextArea from expanding as the user types text into it.
I want the TextArea's width locked.
2.) I want to get each line (or row) of text the user typed in after the sumbit button is hit separately from the whole string of the TextArea. I have not tested out the getText() method, but I'm pretty sure it will return a string of the whole TextArea.
3.) Set a maximum characters per line (4)

Thank you,
Simon Tang.



1. You should be able to set width. textarea1.setWidth(some number);

2. I don't think you can get just a row in the text area, since getText() will return the entire string. But you can break up the giant string into the rows by some simple string manipulation. You'll probably want to find an escape key such as "\n" in the string which would be the same as a return character. If you need help on the stringl manipuation then post again.

3. Hmmm, the only way I can think of would be to have some error checking function after you hit submit. Which would count the number of characters in the whole text area. Taking what you have from my answer in #2 this should be pretty easy.

if (row1.length >= 4)
{
//pop up box to indicate warning
}

Share this post


Link to post
Share on other sites

If you want to limit what the user types into the TextArea, you might want to set up a Document listener on the textarea. As such, whenever the user modifies the contents of the textarea, you can perform some type of validation on the content. An example of DocumentListener implementation follows:public class docListener implements javax.swing.event.DocumentListener { public void insertUpdate(DocumentEvent evt) { validate(evt); } public void removeUpdate(DocumentEvent evt) { validate(evt); } public void changedUpdate(DocumentEvent evt) { validate(evt); } // extra methods public void validate(DocumentEvent evt) { if (evt.getLength()>=4) { // alert user } } }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.