Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

JTextField, input verification

Thread view: 
tereferekuku - 12 Jun 2007 16:14 GMT
Hi,

I've got JFormattedTextField instantiated with RegexFormatter (a class
which does input verification basing on regular expression I supply).
It works fine except for one thing: verification happens after I
remove focus from JTextField instance, and I'd like to have on the
flight verification - happening while an user types consecutive
characters.
For example I have regex recognizing integers; if I type in "12A3" and
remove focus it will reject the input, but I want to have "123"
instead - so that "A" gets ignored.
I also tried to override keyPressed method from KeyListener, but
without success.
Any ideas?

Rgds,
Arek
Tom Hawtin - 12 Jun 2007 18:53 GMT
> I've got JFormattedTextField instantiated with RegexFormatter (a class
> which does input verification basing on regular expression I supply).
[quoted text clipped - 5 lines]
> remove focus it will reject the input, but I want to have "123"
> instead - so that "A" gets ignored.

Yes, JFormattedTextField is junk.

The standard way to tackle the issue is to set a filter on the document
(AbstractDocument.setDocumentFilter). Remove unwanted characters in
DocumentFilter.insertString/replace before passing on to the
DocumentFilter.FilterBypass.

I have an example containing, amongst other things, the technique:

http://www.jroller.com/page/tackline?entry=trivial_swing
http://www.jroller.com/resources/t/tackline/PayCalculator.java
http://www.jroller.com/resources/t/tackline/PayCalculator.jnlp

Tom Hawtin
RedGrittyBrick - 12 Jun 2007 22:43 GMT
Apologies to tereferekuku for this diversion.

> I have an example containing, amongst other things, the technique:
>
> http://www.jroller.com/page/tackline?entry=trivial_swing

An interesting read. You say "GridBag has it's faults, but it's the only
layout manager worth using."

Personally, I'd far rather use MigLayout.

Of the standard[1] Layout Managers, GridBagLayout is the most capable[2]
but I tend to use it as a last resort. Often, nesting the other layout
managers seems to yield acceptable results.

I'm sure you must have explained your preference before but I couldn't
find it - pointers?

Signature

RGB
[1] http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html
[2] GroupLayout and SpringLayout are designed for GUI builders so I
haven't tried them, I'm assuming they don't allow you to do things that
can't be done at all in GridBagLayout.

Tom Hawtin - 13 Jun 2007 00:46 GMT
> Of the standard[1] Layout Managers, GridBagLayout is the most capable[2]
> but I tend to use it as a last resort. Often, nesting the other layout
> managers seems to yield acceptable results.

Actually I use GridBagLayout as first resort. Any kind of Swing
programmer is going to know the peculiarities of GridBagLayout. If you
dive straight in with it, your layout code should be uniform.

> I'm sure you must have explained your preference before but I couldn't
> find it - pointers?

I wrote some entires on specific layout managers:

http://www.jroller.com/page/tackline?entry=cushioning_springlayout
http://www.jroller.com/page/tackline?entry=distinctly_grouplayout
http://www.jroller.com/page/tackline?entry=do_the_funky_formlayout

and about making GridBagLayout and SpringLayout easier to use:

http://www.jroller.com/page/tackline?entry=gridbag_and_spring_layout_helpers

> [2] GroupLayout and SpringLayout are designed for GUI builders so I
> haven't tried them, I'm assuming they don't allow you to do things
> that can't be done at all in GridBagLayout.

SpringLayout is low-level. If you look at the source to GroupLayout
you'll see that it uses (different) springs. The only GUI builder that I
have seen that uses SpringLayout is the "demo" in the Bean Developer Kit
(BDK), IIRC. It doesn't provide abstractions and is just absurd to use.

GroupLayout is a bit of a pain to use by code. I've toyed with the idea
of giving it a facade that allows use of coordinates like GridBagLayout
(but without the hideous API).

They both allow you to do things that you can't in GridBagLayout. For
GroupLayou, you don't necessarily need to know whether one grid line
will be to the left of right of another, so avoiding nested panels. You
can link the sizes of a set of columns, say. SpringLayout is very
flexible and indeed extensible. It's just impossible to use.

Tom Hawtin
tereferekuku - 13 Jun 2007 09:25 GMT
> Apologies to tereferekuku for this diversion.
>
[quoted text clipped - 13 lines]
> I'm sure you must have explained your preference before but I couldn't
> find it - pointers?

To be honest I have no idea what it has to do with my problem (and the
original topic)...

Arek
RedGrittyBrick - 13 Jun 2007 22:14 GMT
>> Apologies to tereferekuku for this diversion.

<diversion snipped>

> To be honest I have no idea what it has to do with my problem (and the
> original topic)...

It has nothing to do with your original problem, it was a diversion.
This sort of thing happens a lot in newsgroup discussions. Sometimes it
seems to divert attention away from the original problem. This was not
my intention but I recognised it  might be an undesired consequence,
hence my apology to you in advance.

Tom has already addressed your original question, I hadn't anything
useful to add.

Have you seen
http://java.sun.com/developer/JDCTechTips/2001/tt1120.html

It discusses the main options for achieving what you want. Towards the
end it discusses using PlainDocument to constrain a JTextField to digits
in, er, realtime. (I like to also emit a beep() for non-digits).

In many cases a JSpinner with a SpinnerNumberModel makes a good
alternative for entering integers.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.