Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Tricky problem with FocusListener
Tricky problem with FocusListener [message #461710] Tue, 27 September 2005 20:57 Go to next message
Philipp Tiedt is currently offline Philipp TiedtFriend
Messages: 52
Registered: July 2009
Member
Hi all,

I have a little tricky problem and hopefully someone has an idea to
solve this. It takes a little time to explain though, sorry for that :-)

Any ideas are welcome.

Suppose I have a text field and a button. The button should only be
enabled when the textfield has focus... (Pressing the button will enter
a string at the current carret position in the textfield)

I added a focuslistener to my textfield and enabled my button when the
focus is gained. Now, when the focus is lost I want to disable the
button obviously. But the focus also gets lost when I try to click the
button. So clicking the button results in focus lost which results in
disabling the button. So what I did is checking whether the button is
the new focuscontrol via myButton.isFocusControl(); if it is the focus
control I do not disable it when the text field looses focus.

That solution worked fine for Windows. However testing on Linux wouldnt
work. If I check myButton.isFocusControl() in my focusLost() method of
the FocusListener it returns false even if I click on the button. I
guess Events are thrown differently here.

So I am looking for a better solution to this problem which does not
depend on the order of events that are reported.

Any ideas would be appreciated.

-philipp
Re: Tricky problem with FocusListener [message #461713 is a reply to message #461710] Wed, 28 September 2005 06:50 Go to previous messageGo to next message
arne anka is currently offline arne ankaFriend
Messages: 133
Registered: July 2009
Senior Member
maybe you're better off, creating _one_ FocusListener-instance with only
focusGained() functional and adding it to all controls which might gain
focus.
now, in focusGained you check for the widget gaining the focus and if the
widget is not your Text or your Button you disable the Button?
probably the idea needs to be more elaborated, but that's up to you ...
Re: Tricky problem with FocusListener [message #461714 is a reply to message #461713] Wed, 28 September 2005 07:42 Go to previous messageGo to next message
Philipp is currently offline PhilippFriend
Messages: 49
Registered: July 2009
Member
This is a multipart message in MIME format.
--=_alternative 002A57F3C125708A_=
Content-Type: text/plain; charset="US-ASCII"

I thought about that before. The problem is, that I cannot add focus
listners to controls that are not in my hand (like controls in other
views). I need the button to be disabled also when those ones gain the
focus.

Thx anyway
-philipp
--=_alternative 002A57F3C125708A_=
Content-Type: text/html; charset="US-ASCII"


<br><font size=2 face="sans-serif">I thought about that before. The problem
is, that I cannot add focus listners to controls that are not in my hand
(like controls in other views). I need the button to be disabled also when
those ones gain the focus.</font>
<br>
<br><font size=2 face="sans-serif">Thx anyway</font>
<br><font size=2 face="sans-serif">-philipp</font>
--=_alternative 002A57F3C125708A_=--
Re: Tricky problem with FocusListener [message #461723 is a reply to message #461714] Wed, 28 September 2005 14:01 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2151
Registered: July 2009
Senior Member
Hi Philipp,

You can be notified of all FocusIns that occur with an application with
Display.addFilter(SWT.FocusIn, yourListener). The only case that this does
not catch is the user clicking out to some other application, since you
won't receive a FocusIn for this.

A more straight-forward solution would be a workaround for the event firing
difference that you originally described. If you defer the disabling of
your Button just slightly with Display.asyncExec(...), shown below, then
your original approach should work everywhere:

text.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent e) {
text.getDisplay().asyncExec(new Runnable() {
public void run() {
if (button.isDisposed()) return;
button.setEnabled(button.isFocusControl());
}
});
}
public void focusGained(FocusEvent e) {
button.setEnabled(true);
}
});

Grant


<philipp_tiedt@de.ibm.com> wrote in message
news:dhdhh7$61b$1@news.eclipse.org...

I thought about that before. The problem is, that I cannot add focus
listners to controls that are not in my hand (like controls in other views).
I need the button to be disabled also when those ones gain the focus.

Thx anyway
-philipp
Previous Topic:converting Swing JTree to SWT Tree
Next Topic:Color of StatusLineManager
Goto Forum:
  


Current Time: Sun Jun 16 16:31:28 GMT 2019

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

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

Back to the top