Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Text validation and setting focus
Text validation and setting focus [message #453863] Tue, 12 April 2005 19:46 Go to next message
Shilpa Toraskar is currently offline Shilpa ToraskarFriend
Messages: 102
Registered: July 2009
Senior Member
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 #453864 is a reply to message #453863] Tue, 12 April 2005 20:11 Go to previous messageGo to next message
James Leotta is currently offline James LeottaFriend
Messages: 202
Registered: July 2009
Senior Member
Take a look at this:
http://www.swtworkbench.com/devzone/edwiki.cgi/ProjectDocume ntation#Installation%20and%20Quick%20Start

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 #453865 is a reply to message #453864] Tue, 12 April 2005 21:15 Go to previous messageGo to next message
steve is currently offline steveFriend
Messages: 6
Registered: July 2009
Junior Member
I don't think I got my question answered, I should have mentioned that I
want to use only standard Eclipse APIs.
Re: Text validation and setting focus [message #453873 is a reply to message #453863] Wed, 13 April 2005 13:45 Go to previous messageGo to next message
Andy Arhelger is currently offline Andy ArhelgerFriend
Messages: 62
Registered: July 2009
Member
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 #453906 is a reply to message #453873] Wed, 13 April 2005 17:33 Go to previous messageGo to next message
Andy Arhelger is currently offline Andy ArhelgerFriend
Messages: 62
Registered: July 2009
Member
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 #453918 is a reply to message #453906] Wed, 13 April 2005 21:41 Go to previous messageGo to next message
Eclipse UserFriend
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 #453922 is a reply to message #453918] Wed, 13 April 2005 23:25 Go to previous messageGo to next message
steve is currently offline steveFriend
Messages: 6
Registered: July 2009
Junior Member
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.
Re: Text validation and setting focus [message #453966 is a reply to message #453922] Fri, 15 April 2005 01:45 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Text validation and setting focus [message #454176 is a reply to message #453966] Mon, 18 April 2005 19:46 Go to previous message
Andy Arhelger is currently offline Andy ArhelgerFriend
Messages: 62
Registered: July 2009
Member
1) Actually focusgained works well, at least so far in my testing. I
also need the item that focus is moving to so I can know when to ingnor
the validity checking. Like clicking on a Cancel button. At focusLost
time you get way to many events. Even showing another window on your
desktop generates the focusLost, like ALT-TAB. You really don't want to
do validity checking in this case.
2) You have to use a focusListener becase traverseListener doesn't
doesn't get called for mouse clicks.
3) For the ALT-TAB case the focus remains in the control you were
editing so when you come back later to that window the focus hasn't moved.

Andy Arhelger

Bob Foster wrote:
> 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
Previous Topic:prevent Exit Event from Text
Next Topic:Strange Shell behaviour
Goto Forum:
  


Current Time: Fri Apr 19 21:32:40 GMT 2024

Powered by FUDForum. Page generated in 0.03198 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top