• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating fields during cancel  RSS feed

 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • 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: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never used ANTLR, but I'll check it out. Thanks Chantal.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!