Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Selection in Text and mouse events
Selection in Text and mouse events [message #509652] Sun, 24 January 2010 10:25 Go to next message
Peter Kanzler is currently offline Peter KanzlerFriend
Messages: 9
Registered: July 2009
Junior Member
This is a multi-part message in MIME format.
--------------080505050806060807070206
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I want to select all the content of a Text when it gets the focus. It's
no problem if you use the TAB key to set the focus to the Text widget.
But if you select it with your mouse cursor the content doesn't get
selected. I want to to give the user the possibility to set the caret in
any place in the textfield once the field has the focus. This is why I
can't just use text.selectAll() in the MouseUp event.

I used a FocusListener and MouseListener to make this happen. Sadly the
selection gets lost somewhere between the MouseDown and MouseUp event. I
can't explain why and how. I guess I am missing something simple...

I included a snippet so you can understand my problem more easily.
1. Start it
2. Click with your mouse into the first textfield. The whole content
should have been selected. But it won't.

cheers
Peter

--------------080505050806060807070206
Content-Type: text/plain;
name="SWTText.java"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="SWTText.java"

aW1wb3J0IG9yZy5lY2xpcHNlLnN3dC5TV1Q7CmltcG9ydCBvcmcuZWNsaXBz ZS5zd3QuZXZl
bnRzLkZvY3VzQWRhcHRlcjsKaW1wb3J0IG9yZy5lY2xpcHNlLnN3dC5ldmVu dHMuRm9jdXNF
dmVudDsKaW1wb3J0IG9yZy5lY2xpcHNlLnN3dC5ldmVudHMuTW91c2VBZGFw dGVyOwppbXBv
cnQgb3JnLmVjbGlwc2Uuc3d0LmV2ZW50cy5Nb3VzZUV2ZW50OwppbXBvcnQg b3JnLmVjbGlw
c2Uuc3d0LmxheW91dC5HcmlkRGF0YTsKaW1wb3J0IG9yZy5lY2xpcHNlLnN3 dC5sYXlvdXQu
R3JpZExheW91dDsKaW1wb3J0IG9yZy5lY2xpcHNlLnN3dC53aWRnZXRzLkRp c3BsYXk7Cmlt
cG9ydCBvcm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2VEb3duKE1vdXNlRXZlbnQgZSkgewoJ CQkJCVN5c3Rl
bS5vdXQucHJpbnRsbigiTW91c2VEb3duOiAiICsgdGV4dC5nZXRTZWxlY3Rp b24oKSk7CgkJ
CQkJdGV4dC5zZXRTZWxlY3Rpb24odGV4dC5nZXRTZWxlY3Rpb24oKSk7CgkJ CQl9IAoJCQkJ
CgkJCQlAT3ZlcnJpZGUgCgkJCQlwdWJsaWMgdm9pZCBtb3VzZVVwKE1vdXNl RXZlbnQgZSkg
ewoJCQkJCVN5c3RlbS5vdXQucHJpbnRsbigiTW91c2VVcDogIiArIHRleHQu Z2V0U2VsZWN0
aW9uKCkpOwoJCQkJCXRleHQuc2V0U2VsZWN0aW9uKHRleHQuZ2V0U2VsZWN0 aW9uKCkpOwkJ
CQkJCgkJCQl9CgoJCQl9KTsKCQkJCgkJCWZpbmFsIFRleHQgb3RoZXJUZXh0 ID0gbmV3IFRl
eHQoc2hlbGwsIFNXVC5CT1JERVIgfCBTV1QuUklHSFQpOwoJCQlvdGhlclRl eHQuc2V0TGF5
b3V0RGF0YShuZXcgR3JpZERhdGEoR3JpZERhdGEuRklMTF9IT1JJWk9OVEFM KSk7CgkJCQoJ
CQlvdGhlclRleHQuc2V0Rm9jdXMoKTsKCQkJCgkJCXNoZWxsLm9wZW4oKTsK CQkJd2hpbGUg
KCFzaGVsbC5pc0Rpc3Bvc2VkKCkpIHsKCQkJCWlmICghZGlzcGxheS5yZWFk QW5kRGlzcGF0
Y2goKSkKCQkJCQlkaXNwbGF5LnNsZWVwKCk7CgkJCX0KCQkJZGlzcGxheS5k aXNwb3NlKCk7
CgkJfQoJCWNhdGNoIChFeGNlcHRpb24gZSkgewoJCQllLnByaW50U3RhY2tU cmFjZSgpOwoJ
CX0KCX0KCn0=
--------------080505050806060807070206--
Re: Selection in Text and mouse events [message #509858 is a reply to message #509652] Mon, 25 January 2010 15:47 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2151
Registered: July 2009
Senior Member
Hi Peter,

A listener like the one below will delay the text selection until the after
the mouse events have been processed.

text.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent arg0) {
display.asyncExec(new Runnable() {
public void run() {
if (text.isDisposed()) return;
text.selectAll();
}
});
}
});

I'm not sure what you mean by the user having the possibility of setting the
caret position anywhere within the Text. Do you mean instead of selecting
all the text? In the single selection model, after text is selected I don't
think the caret can be anywhere other than on one of the ends of the
selected text.

Grant


"Peter Kanzler" <derkanzler@gmx.at> wrote in message
news:hjh777$b9c$1@build.eclipse.org...
> Hi,
>
> I want to select all the content of a Text when it gets the focus. It's
> no problem if you use the TAB key to set the focus to the Text widget.
> But if you select it with your mouse cursor the content doesn't get
> selected. I want to to give the user the possibility to set the caret in
> any place in the textfield once the field has the focus. This is why I
> can't just use text.selectAll() in the MouseUp event.
>
> I used a FocusListener and MouseListener to make this happen. Sadly the
> selection gets lost somewhere between the MouseDown and MouseUp event. I
> can't explain why and how. I guess I am missing something simple...
>
> I included a snippet so you can understand my problem more easily.
> 1. Start it
> 2. Click with your mouse into the first textfield. The whole content
> should have been selected. But it won't.
>
> cheers
> Peter
>
Previous Topic:An annoying tree control problem
Next Topic:FormLayout and child relative height
Goto Forum:
  


Current Time: Fri Nov 28 01:37:57 GMT 2014

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

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