Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg. Validatin JTextFields

 
mohan rajv
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any option for restricting the number of characters in the jtextfield
there s a option called setcolumns(int ).if i set that as 5.(new JTextField(5);like this)..but it is accepting more than 5 characters.
how to resolve this.?
thanks in advance..
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohan,

In future please do not use big fonts as it makes the post hard to read. If you want to draw attention to a portion of your post, you can use the italics or bold.

The setColumn() method is used to control the visible width of the text field.
Check out this example which should point you to how you can achieve your functionality.
Check out the Document class.
You can use that to create a custom document and use the insertString() in the Document class to control the accepted text width.
Once you have this document you will need to attach it to your text field.
 
mohan rajv
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thans Maneesh Godbole ..
how can i attach the document with the text field.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setDocument.
 
mohan rajv
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to create Document object?
i saw in a sample program they created document object using abstractdocument (by getstyleddocument method of jtextpane).in my prog i am not using the textpane and all.then how to create the doucument object.
can anyone give the sample code?
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Override PlainDocument. That's the one currently used in JTextFields.
 
mohan rajv
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks rob,,
but can you give sample code, or explain about that plaindocument overriding..
i dont know about that.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Information about the PlainDocument can be found in the API docs.
In my previous post I had told you the name of the method you need to tweak.
In his previous post Rob has told you how to attach the custom document to the TextField.

Which of these points are you having trouble understanding?
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know how to extend a class, right?
You know how to override a method, right?

Simply override insertString like this (pseudo code):

I'm quite sure you can find out how to retrieve the current length. Just check the API.
The maximum length is something you will need to specify; either as an instance field, or as a constant (static final).
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohan raj v wrote:Is there any option for restricting the number of characters in the jtextfield
there s a option called setcolumns(int ).if i set that as 5.(new JTextField(5);like this)..but it is accepting more than 5 characters.
how to resolve this.?


The setColumns() method has no effect on the content of the field, just on its layout (its preferred size).

It turns out there are a lot of ways to restrict the length of the field's Document. Some have been mentioned in this thread. Since JDK 1.4, though, I think the preferred way is to use a DocumentFilter.
 
Maja Gralewska
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maneesh, i looked at the document class but to me this seemed like a slightly easer way to create it as it is an array?// Sorry i posted this on this thread by accident was supposed to be posted on JTextField set size limit.
 
Prashant N. Mahajan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all.......

i found in one of the post the following code information about PlainDocument class....... so implemented following ........it worked fine........


=============================================

import javax.swing.text.*;

public class CheckLength extends PlainDocument
{
public CheckLength()
{
}

public void insertString (int offset, String s, AttributeSet attributeSet) throws BadLocationException
{
try {
if (offset <1)
super.insertString(offset, s, attributeSet);
}
catch (Exception e) {
}
}
}


=============================================

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

public class JTextFieldDemo
{
JTextField jTf = new JTextField();
CheckLength cl = new CheckLength();
JFrame f = new JFrame();
public JTextFieldDemo()
{
Container c = f.getContentPane();

jTf.setDocument(cl);
c.add(jTf);

f.setVisible(true);
f.setSize(100,100);
}
public static void main(String args[])
{
new JTextFieldDemo();
}

}

=============================================

thnks to the poster who gave info abt PlainDocument class.........
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you notice this thread was half a year old? Also, next time please Use Code Tags. And read your private messages.
 
Rob Camick
Ranch Hand
Posts: 2583
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, as stated earlier, the preferred approach is to use a DocumentFilter, not a custom Document.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic