File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Need to limit the text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need to limit the text" Watch "Need to limit the text" New topic
Author

Need to limit the text

Kajol Shroff
Ranch Hand

Joined: Dec 04, 2000
Posts: 160
Hi Everybody again,
I have a problem........
I need to limit the text inputed by the user in the textfield..i mean i set the size of the textfield to say 25 and i need to limit the max char or max int to say 50...
How do i do it in JAVA .....
Thanks in advance
Kajol
Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
You need to create a custom class that extends PlainDocument and set it using JTextField.setDocument(). The class will listen in and check the number of characters for you. All you need to do is set the limit. You'll need to alter the method insertString() to do the check for you. This is part of a class I implemented that limited the text to 50 characters, and turned all entered text into upper case as well:

Please let me know if you need anything clarified.
[This message has been edited by Matt Senecal (edited March 30, 2001).]


There's nothing more dangerous than a resourceful idiot. ---Dilbert
Kajol Shroff
Ranch Hand

Joined: Dec 04, 2000
Posts: 160
Hi Matt,
i am sorry i ddint get u can u elaborate me on this ..
I need to implement it.........
Thanks in advance
Kajol
Vani Prabakaran
Greenhorn

Joined: Jun 08, 2000
Posts: 13
I don't know whether you found the solution already. Just in case, the below code works fine for me.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyTextField extends JTextField{
int limit;
public MyTextField() {
}
public MyTextField(int show,int limit) {
super(show);
this.limit = limit;
addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt) {
if (getText().length() >= MyTextField.this.limit)
evt.consume();
}
});
}
}
Let me know if you have any questions.
Vani
Kajol Shroff
Ranch Hand

Joined: Dec 04, 2000
Posts: 160
Hi vani,
Thanks for ur reply...........
Well can u tell me wht if i dont use Swings and am extending Applet insteadof TextField........
How do i go about it.....
Kajol
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

You main "application" (or applet) can extend applet, but what Vani is showing you is how to extend a JTextField. So instead of using a JTextField, you would now use MyTextField.

Here is an example of an application that uses two different kinds of extended JTextFields.

And thanks to Vani! I was unable to get my own 'limited' textbox to work using Documents. This solution seems much simpler. I'm waiting for the thorn though!

Here is the class that Vani gave you. It's been modified to use AWT.
here is the class for UpperCaseFieldAnd finally, here is a class that uses them all. In your case Kajol, this would be an applet.

[This message has been edited by Mike Curwen (edited April 04, 2001).]
Kajol Shroff
Ranch Hand

Joined: Dec 04, 2000
Posts: 160
Hi Mike/ Matt,
Sorry I couldnt get your example can u please explain me using without Swing as I have not reached till swing in my studies
Waiting for more replies
Kajol
Jahanzeb Sayal
Greenhorn

Joined: Mar 02, 2001
Posts: 21
Hii Kajol...
Thanks for replying...
As far as awt is concerned you can do it manually bcoz in awt OS widget is used and you have a little flexibility.
You can limit the size of display by using the constructor of TextField as follows
TextField(int numChars)
Here numChars speicifies the size of TextField in size.
As far as limiting the amount is concerned as far as i can do...
There are two methods
1)After user has input the value u can check it by calling String class length() method.
2)The other way that u can do it by using the TextEvent.
Note:
The second method is burdebsome becasuse all the times the text is changed in textfield the code in the TextEvent will check the String length.. But it will check at the input level..
With First method u can also check at the input level by using the FocusEvent i.e when the textfield lose focus u can check the length of field...
Tomorrow i will also post the code.


Sayal
Kajol Shroff
Ranch Hand

Joined: Dec 04, 2000
Posts: 160
Thanks Jahanzeb Sayal,
For ur reply..
That was of real help
Kajol
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Kajol: If you look closely, the code i supplied in the first code snip does not use Swing.

Vani: If you are looking at this, could you please answer this question? It concerns your code. http://www.javaranch.com/ubb/Forum1/HTML/001212.html
Drake Silver
Ranch Hand

Joined: Apr 21, 2002
Posts: 80
Originally posted by Mike Curwen:
You main "application" (or applet) can extend applet, but what Vani is showing you is how to extend a JTextField. So instead of using a JTextField, you would now use MyTextField.

Here is an example of an application that uses two different kinds of extended JTextFields.

And thanks to Vani! I was unable to get my own 'limited' textbox to work using Documents. This solution seems much simpler. I'm waiting for the thorn though!

Here is the class that Vani gave you. It's been modified to use AWT.
here is the class for UpperCaseFieldAnd finally, here is a class that uses them all. In your case Kajol, this would be an applet.

[This message has been edited by Mike Curwen (edited April 04, 2001).]

Mike:
Is it safe to assume that with the first method, block of code, you created that extends textfield, that if I wanted to limit the amount of text in text area, or textpane or even text editor, I would only need to change the extends textfield, to extends text area and so forth?


"Shh, Zog....here come one Now!"
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
Just some comments...
I didn't try it, but the suggestion using the Document of the TextComponent for limiting input is the more oop-way to go. the text component (JTextField, JTextPane etc) is only concerned with the view while the Document classes manage the data of these compoennts. thus, you could use one Document subclass for different text components, limiting the input of each of them.
Another possibility in Swing is the JFormattedField.
cheers
Chantal
Rambo Rambabu
Greenhorn

Joined: Jul 12, 2002
Posts: 21
Hi,
I tried to implement this feature using addKeyListener and extending JTextField. It is working fine as far as limiting the size of the text. Once it reaches the max size, my backspace key is not working. I think the call to comsume() is consuming backspace key also. I added an extra condition to the if clause:
&& (!(evt.getKeyChar() == '')) ) // check for backspace key stroke and allow it
After adding this line, backspace works fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to limit the text
 
Similar Threads
pass parameter from text box to applet
Bringing up a calender
limiting input from a textfield???
re-enable buttons
Font resizing issue in PDF using IText Library