|
|
|
|
|
Re: Text validation and setting focus [message #453918 is a reply to message #453906] |
Wed, 13 April 2005 21:41 |
Eclipse User |
|
|
|
Originally posted by: bob.objfac.com
I'm a little dubious one could get validity checking working properly by
checking on focus gained. The problem you had with clicking on the
window X button should be easy enough to deal with by setting up a
ShellListener to listen for the close event, set a "closing" flag for
your validation method so it doesn't try to setFocus() in this case.
Bob Foster
Andy Arhelger wrote:
> Actually I tried some more on this and got it working.
> I ended up doing the validity checking on the focusGained method. In the
> focusLost I just save the control which lost focus for later use.
> I also needed to display the message dialog in an asyncExec. I haven't
> tried all cases but it appears to work.
>
> Andy Arhelger
>
>
> Andy Arhelger wrote:
>
>> I would be interested in finding a way to do this as well. I
>> experemented like you did and found the focus lost method almost
>> works. If you show the message box then set focus back on the failing
>> component this way:
>>
>> final Control c = control;
>> control.getDisplay().asyncExec (new Runnable () {
>> public void run () {
>> c.setFocus();
>> }
>> });
>>
>> This worked for most cases except when clicking on the window X button
>> to close the window when it gets stuck in an infinite loop showing the
>> same error message. I debugged this some and could't figure out a way
>> around it.
>>
>> Andy Arhelger
>>
>>
>> steve wrote:
>>
>>> I am implementating a wizard page which has bunch of text controls. I
>>> need to do application specific validation on text controls, display
>>> an error message if validation fails and set control/focus back to
>>> that text control. I tried focusListener as well as TraverseListener.
>>> In case of traverselistener, user can tab out and I can catch those
>>> events, which is fine. But user can also use mouse to go to next
>>> field. So ideal is using focusListener but I don't know how to set
>>> focus back to that field control.setFocus doesn't work as I expected.
>>> It keeps popping up error message depending on how many controls I
>>> have on that page. Any help is greatly appreciated...
>>>
>>> Here is sample code
>>> ctrl_name.addTraverseListener(new TraverseListener() {
>>> public void keyTraversed(TraverseEvent e) {
>>> if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail ==
>>> SWT.TRAVERSE_TAB_PREVIOUS ) {
>>> String msg = doValidation(); if
>>> (msg == null)
>>> {
>>> e.doit = true;
>>> }
>>> else
>>> {
>>> MsgBox.showError(msg);
>>> e.doit = false;
>>> }
>>> }
>>> }
>>> });
>>>
>>> OR
>>>
>>> ctrl_name.addFocusListener(new FocusAdapter() {
>>> public void focusLost(FocusEvent e) {
>>> String msg =
>>> doValidation(); if (msg == null)
>>> {
>>> // do nothing
>>> }
>>> else
>>> {
>>> MsgBox.showError(msg);
>>> ctrl_name.setFocus();
>>> }
>>> }
>>> });
>>>
>>>
>>>
|
|
|
|
Re: Text validation and setting focus [message #453966 is a reply to message #453922] |
Fri, 15 April 2005 01:45 |
Eclipse User |
|
|
|
Originally posted by: bob.objfac.com
steve wrote:
> Actually by setting focusListener may be a bad idea, since
> ALT+TAB(switching to another application) will also cause focusLost
> event and will display error message box. I think TraverseListener is
> much better way to go. But I still need to figure out how to capture if
> user uses mouse to move out of that text box.
I think it's a bad idea to let the user leave invalid data in a text
box. Then when do you check it? Better to display the error dialog,
which will be modal but not across applications and will be in front of
the parent shell when the user switches back.
Bob Foster
|
|
|
|
Powered by
FUDForum. Page generated in 0.03422 seconds