aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Reg. Validatin JTextFields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Reg. Validatin JTextFields" Watch "Reg. Validatin JTextFields" New topic
Author

Reg. Validatin JTextFields

mohan rajv
Ranch Hand

Joined: Jan 28, 2009
Posts: 30
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

Joined: Jul 26, 2007
Posts: 10372
    
    8

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.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
mohan rajv
Ranch Hand

Joined: Jan 28, 2009
Posts: 30
thans Maneesh Godbole ..
how can i attach the document with the text field.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

setDocument.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
mohan rajv
Ranch Hand

Joined: Jan 28, 2009
Posts: 30
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

Override PlainDocument. That's the one currently used in JTextFields.
mohan rajv
Ranch Hand

Joined: Jan 28, 2009
Posts: 30
thanks rob,,
but can you give sample code, or explain about that plaindocument overriding..
i dont know about that.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10372
    
    8

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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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

Joined: Sep 20, 2005
Posts: 862
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.


bitguru blog
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
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.


Potrzeba jest matka wynalazków
Prashant N. Mahajan
Greenhorn

Joined: Sep 07, 2009
Posts: 1
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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

Joined: Jun 13, 2009
Posts: 2182
    
    7
Also, as stated earlier, the preferred approach is to use a DocumentFilter, not a custom Document.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg. Validatin JTextFields