JTextField, input verification

T

tereferekuku

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
 
T

Tom Hawtin

tereferekuku said:
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.

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
 
R

RedGrittyBrick

Apologies to tereferekuku for this diversion.

Tom said:
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?

--
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.
 
T

Tom Hawtin

RedGrittyBrick said:
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
 
T

tereferekuku

Apologies to tereferekuku for this diversion.

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

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?

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

Arek
 
R

RedGrittyBrick

tereferekuku said:
RedGrittyBrick wrote:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top