• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Validating fields during cancel

 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When collecting input data in a GUI, you have two basic strategies for validation. You can wait until all fields have been entered, and validate everything at once, or you can validate it field by field as it's being entered.
In Swing, you can use FocusLost events for the field-by-field validation strategy. However, if your entry is taking place on a dialog with a cancel button, pressing on the cancel button can also generate a focus lost event. I would like to avoid forcing users to fix input on the current field if they are just trying to cancel the input altogether. Has anybody solved this problem? It's not unique to Swing, so it seems like there must be a general strategy.
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Greg,
if you want to stick with focusLost - the only solution that comes to my mind is to check in this listener method whether "cancel" has been clicked. Though I think that won't work as the focus is lost before the button is clicked.
If you don't have to stick with focusLost, use the document events of the text fields to check the input, or use JFormattedField, or use a generated Parser (from ANTLR, e.g.). At first glance these solutions might mean more overhead than listening to focusLost but I don't think that this is noticeable. the advantage of controlling the input as it is typed in, is that you can give the user an immediate feedback - in fact this has the consequence that the interface is more responsive to user input and thus seems "faster". of course the typing performance must not suffer from it.
I am using an ANTLR generated parser for controlling a text field. While the input is invalid, the entered text is displayed in red, as soon as the input is valid the text is displayed in black. this works very fine.
regards,
Chantal
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never used ANTLR, but I'll check it out. Thanks Chantal.
reply
    Bookmark Topic Watch Topic
  • New Topic