Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #13417

Re: inputvalidation of inputdialog

Path csiph.com!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From markspace <-@.>
Newsgroups comp.lang.java.programmer
Subject Re: inputvalidation of inputdialog
Date Thu, 05 Apr 2012 09:31:20 -0700
Organization A noiseless patient Spider
Lines 55
Message-ID <jlkhcq$l53$1@dont-email.me> (permalink)
References <4f7d94b5$0$7609$9b4e6d93@newsspool1.arcor-online.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Thu, 5 Apr 2012 16:31:22 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="QNqT5u6Ryx/DB+LpnoCWdQ"; logging-data="21667"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Hg68+PHGbbFLrCzx0Aj2Dq87qZ6tRIb0="
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20120327 Thunderbird/11.0.1
In-Reply-To <4f7d94b5$0$7609$9b4e6d93@newsspool1.arcor-online.net>
Cancel-Lock sha1:65mamIMUcO91LZFJRj5B00vvN78=
Xref csiph.com comp.lang.java.programmer:13417

Show key headers only | View raw


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>

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

inputvalidation of inputdialog Mark Sudau <news@sudau.net> - 2012-04-05 14:48 +0200
  Re: inputvalidation of inputdialog markspace <-@.> - 2012-04-05 09:31 -0700

csiph-web