Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > inputvalidation of inputdialog

Reply
Thread Tools

inputvalidation of inputdialog

 
 
Mark Sudau
Guest
Posts: n/a
 
      04-05-2012
Hi folks,

I need an InputDialog where I can validate the the users input. The
validation is variable to the belongings of business rules. The
validation my be a number, regular expression, length or some other kind
of validation I actually haven't thought of.

It is not wanted to enter data and validate after pressing ok. The
validation must be done immediately after pressing a key.

I already thought of a KeyListener but using a KeyListener makes it
difficult to validate the length of a regular expression.

Does any one have an idea?

Kind regards
Mark
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      04-05-2012
On 4/5/2012 5:48 AM, Mark Sudau wrote:
> I already thought of a KeyListener but using a KeyListener makes it
> difficult to validate the length of a regular expression.



A key listener is probably the wrong direction entirely. For length,
make a Document and set the text field's document to that, or use a
document listener, or use a DocumentFilter.

Something like (untested):

DocumentFilter filter = new DocumentFilter() {
private final int MAX_LEN = 42;
public void insertString( FilterBypass fb,
int offset, String string, AttributeSet att )
{
if( fb.getDocument().getLength() + string.getLength()
< MAX_LEN )
{
fb.insertString( offset, string, attr );
}
}
public void replaceString( FilterBypass fb,
int offset, String string, AttributeSet att )
{
fb.replaceString( offset, string, attr );
}
};


In general, use the validators that Java already provides, don't roll
your own.

<http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html>

(You have to scroll down a bit to get to the part about validating
input. It's there though, honest.)


Other links:


<http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html>

<http://docs.oracle.com/javase/7/docs/api/javax/swing/InputVerifier.html>

<http://docs.oracle.com/javase/7/docs/api/javax/swing/text/DocumentFilter.html>

PlainDocument is an AbstractDocument that you could easily extend. I
think PlainDocument is the model used for all plain text fields in Swing
as well as the unformatted (plain) text areas.

<http://docs.oracle.com/javase/7/docs/api/javax/swing/text/PlainDocument.html>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get a string using InputDialog with FxRuby Li Chen Ruby 9 09-29-2008 02:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57