f



GUI and events in text field

Hi again, it has been a while since I posted here. I came here a while
back when I had a beginner's course in Java and again when I was
practicing my Java. I've finally managed to get into a more advanced
Java class and I need some guidance.

We have to create a GUI that validates some fields. For example, a
date field so that everything is numeric and within the right ranges
and so on. Can someone please write me one and send it to me?

Just Kidding!

Actually, I've written the first version of it, but it is not the way
I want it to be, hence my question.

I want to validate each field as the user finishes typing in that
field. If I understood the instructor correctly, that means I have to
monitor each keystroke in the field, but this sounds weird. I can see
how I can then interrupd and avoid  text being entered into a nnumeric
field, but I think one should ony verify the field when the user tries
to leave it.

Now I think I can track that by checking if a field has focus or is
losing focus. Am I correct or is there a better way to track a field?

Is the event of losing focus something I implement in the
ActionListener? Or is it a specific event that I need to listen for?

Thanks for all advice.
MikeB

0
MPBrede (152)
2/8/2007 8:44:58 PM
comp.lang.java.help 13339 articles. 0 followers. Post Follow

4 Replies
452 Views

Similar Articles

[PageSpeed] 3

MikeB wrote:
> Hi again, it has been a while since I posted here. I came here a while
> back when I had a beginner's course in Java and again when I was
> practicing my Java. I've finally managed to get into a more advanced
> Java class and I need some guidance.
> 
> We have to create a GUI that validates some fields. For example, a
> date field so that everything is numeric and within the right ranges
> and so on. Can someone please write me one and send it to me?
> 
> Just Kidding!
> 
> Actually, I've written the first version of it, but it is not the way
> I want it to be, hence my question.
> 
> I want to validate each field as the user finishes typing in that
> field. If I understood the instructor correctly, that means I have to
> monitor each keystroke in the field, but this sounds weird. I can see
> how I can then interrupd and avoid  text being entered into a nnumeric
> field, but I think one should ony verify the field when the user tries
> to leave it.
> 
> Now I think I can track that by checking if a field has focus or is
> losing focus. Am I correct or is there a better way to track a field?
> 
> Is the event of losing focus something I implement in the
> ActionListener? Or is it a specific event that I need to listen for?
> 
> Thanks for all advice.
> MikeB
> 

Kind of depends if you are using AWT or Swing components.  With AWT you 
probably need to check each keystroke along with the finished field. 
With Swing there are some neat classes like InputVerifier to keep you 
from leaving a field and Document to limit the type of text data that is 
entered into a field.

-- 

Knute Johnson
email s/nospam/knute/
0
nospam8071 (1252)
2/8/2007 10:29:24 PM
On Feb 8, 4:29 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> MikeB wrote:
> > Hi again, it has been a while since I posted here. I came here a while
> > back when I had a beginner's course in Java and again when I was
> > practicing my Java. I've finally managed to get into a more advanced
> > Java class and I need some guidance.
>
> > We have to create a GUI that validates some fields. For example, a
> > date field so that everything is numeric and within the right ranges
> > and so on. Can someone please write me one and send it to me?
>
> > Just Kidding!
>
> > Actually, I've written the first version of it, but it is not the way
> > I want it to be, hence my question.
>
> > I want to validate each field as the user finishes typing in that
> > field. If I understood the instructor correctly, that means I have to
> > monitor each keystroke in the field, but this sounds weird. I can see
> > how I can then interrupd and avoid  text being entered into a nnumeric
> > field, but I think one should ony verify the field when the user tries
> > to leave it.
>
> > Now I think I can track that by checking if a field has focus or is
> > losing focus. Am I correct or is there a better way to track a field?
>
> > Is the event of losing focus something I implement in the
> > ActionListener? Or is it a specific event that I need to listen for?
>
> > Thanks for all advice.
> > MikeB
>
> Kind of depends if you are using AWT or Swing components.  With AWT you
> probably need to check each keystroke along with the finished field.
> With Swing there are some neat classes like InputVerifier to keep you
> from leaving a field and Document to limit the type of text data that is
> entered into a field.
>
> --
>
> Knute Johnson
> email s/nospam/knute/- Hide quoted text -
>
> - Show quoted text -

Sorry, I should have specified. We're using NetBeans as IDE and are
developing in Swing. I'll look into the InputVerifier class. Thanks

0
MPBrede (152)
2/8/2007 10:49:51 PM
MikeB wrote:
> On Feb 8, 4:29 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
>> MikeB wrote:
>>> Hi again, it has been a while since I posted here. I came here a while
>>> back when I had a beginner's course in Java and again when I was
>>> practicing my Java. I've finally managed to get into a more advanced
>>> Java class and I need some guidance.
>>> We have to create a GUI that validates some fields. For example, a
>>> date field so that everything is numeric and within the right ranges
>>> and so on. Can someone please write me one and send it to me?
>>> Just Kidding!
>>> Actually, I've written the first version of it, but it is not the way
>>> I want it to be, hence my question.
>>> I want to validate each field as the user finishes typing in that
>>> field. If I understood the instructor correctly, that means I have to
>>> monitor each keystroke in the field, but this sounds weird. I can see
>>> how I can then interrupd and avoid  text being entered into a nnumeric
>>> field, but I think one should ony verify the field when the user tries
>>> to leave it.
>>> Now I think I can track that by checking if a field has focus or is
>>> losing focus. Am I correct or is there a better way to track a field?
>>> Is the event of losing focus something I implement in the
>>> ActionListener? Or is it a specific event that I need to listen for?
>>> Thanks for all advice.
>>> MikeB
>> Kind of depends if you are using AWT or Swing components.  With AWT you
>> probably need to check each keystroke along with the finished field.
>> With Swing there are some neat classes like InputVerifier to keep you
>> from leaving a field and Document to limit the type of text data that is
>> entered into a field.
>>
>> --
>>
>> Knute Johnson
>> email s/nospam/knute/- Hide quoted text -
>>
>> - Show quoted text -
> 
> Sorry, I should have specified. We're using NetBeans as IDE and are
> developing in Swing. I'll look into the InputVerifier class. Thanks
> 

And Document can be used very easily to limit input length or type of 
characters.  See PlainDocument.

-- 

Knute Johnson
email s/nospam/knute/
0
nospam8071 (1252)
2/8/2007 11:49:37 PM
MikeB wrote:

> On Feb 8, 4:29 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
>> MikeB wrote:
>> > Hi again, it has been a while since I posted here. I came here a while
>> > back when I had a beginner's course in Java and again when I was
>> > practicing my Java. I've finally managed to get into a more advanced
>> > Java class and I need some guidance.
>>
>> > We have to create a GUI that validates some fields. For example, a
>> > date field so that everything is numeric and within the right ranges
>> > and so on. Can someone please write me one and send it to me?
>>
>> > Just Kidding!
>>
>> > Actually, I've written the first version of it, but it is not the way
>> > I want it to be, hence my question.
>>
>> > I want to validate each field as the user finishes typing in that
>> > field. If I understood the instructor correctly, that means I have to
>> > monitor each keystroke in the field, but this sounds weird. I can see
>> > how I can then interrupd and avoid  text being entered into a nnumeric
>> > field, but I think one should ony verify the field when the user tries
>> > to leave it.
>>
>> > Now I think I can track that by checking if a field has focus or is
>> > losing focus. Am I correct or is there a better way to track a field?
>>
>> > Is the event of losing focus something I implement in the
>> > ActionListener? Or is it a specific event that I need to listen for?
>>
>> > Thanks for all advice.
>> > MikeB
>>
>> Kind of depends if you are using AWT or Swing components.  With AWT you
>> probably need to check each keystroke along with the finished field.
>> With Swing there are some neat classes like InputVerifier to keep you
>> from leaving a field and Document to limit the type of text data that is
>> entered into a field.
>>
>> --
>>
>> Knute Johnson
>> email s/nospam/knute/- Hide quoted text -
>>
>> - Show quoted text -
> 
> Sorry, I should have specified. We're using NetBeans as IDE and are
> developing in Swing. I'll look into the InputVerifier class. Thanks


There's a small section in the Swing Tutorial which explains input verification
using InputVerifier along with Focus events which might be of use to you:

<http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html#inputVerification>

I'm not sure whether a JFormattedTextField can be used to restrict the
characters which can be entered into it, but it might also be worth a look:

<http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html>

-- 
Nigel Wade, System Administrator, Space Plasma Physics Group,
            University of Leicester, Leicester, LE1 7RH, UK 
E-mail :    nmw@ion.le.ac.uk 
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555
0
nmw (873)
2/9/2007 11:50:48 AM
Reply: