Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to limit the text

 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jahanzeb Sayal,
For ur reply..
That was of real help
Kajol
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic