Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » selectAll( ) does not work on Text when the widget gets focus with Mouse click.
selectAll( ) does not work on Text when the widget gets focus with Mouse click. [message #443457] Fri, 24 September 2004 17:39 Go to next message
Eclipse UserFriend
Originally posted by: friends4u20022002.yahoo.com

I have a peculiar problem while trying to get focus on a Text field using
a mouse click, and was wondering if this was a Bug in SWT. Basically, when
I get focus on a particular text field using a mouse click on that text
field, I want to perform a selectAll() on it. This does not seem to work
eventhough focusgained() is being invoked. However, the TAB key invokes
focusgained() and also performs selectAll(). Here is the sample code for
focusgained which is called on the listener using the Mouse click and TAB
key when focus is gained.

public void focusGained(FocusEvent e)
{
//focusGained() gets ivoked for the Mouse Click and Tab

System.out.println("Focus gained");

if(e.getSource() instanceof Text)
{
hrTxt.selectAll(); //Gets invoked for TAB and not Mouse click.
}
}
Re: selectAll( ) does not work on Text when the widget gets focus with Mouse click. [message #443578 is a reply to message #443457] Tue, 28 September 2004 15:42 Go to previous messageGo to next message
Kalman Hazins is currently offline Kalman HazinsFriend
Messages: 76
Registered: July 2009
Member
sounds strange, but you can always create a mouse listener to handle that.
and not to duplicate the code, create a method (which will have selectAll()
and whatever else in it) and call the method from both listeners ...

- Kalman


"Ks" <friends4u20022002@yahoo.com> wrote in message
news:cj1m59$jib$1@eclipse.org...
> I have a peculiar problem while trying to get focus on a Text field using
> a mouse click, and was wondering if this was a Bug in SWT. Basically, when
> I get focus on a particular text field using a mouse click on that text
> field, I want to perform a selectAll() on it. This does not seem to work
> eventhough focusgained() is being invoked. However, the TAB key invokes
> focusgained() and also performs selectAll(). Here is the sample code for
> focusgained which is called on the listener using the Mouse click and TAB
> key when focus is gained.
>
> public void focusGained(FocusEvent e)
> {
> //focusGained() gets ivoked for the Mouse Click and Tab
>
> System.out.println("Focus gained");
>
> if(e.getSource() instanceof Text)
> {
> hrTxt.selectAll(); //Gets invoked for TAB and not Mouse click.
> }
> }
>
Re: selectAll( ) does not work on Text when the widget gets focus with Mouse click. [message #443709 is a reply to message #443457] Wed, 29 September 2004 15:04 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
When you click in a text field with the mouse, whether it already has focus
or not, the place that you click becomes the insertion point and all
selection is cleared. This is expected behaviour.
What is happening in your case is that first there is a focus gained event
and then the Mouse Down event is processed. The Mouse Down event clears the
selection (even if you set it programatically in the focus gained event).
This is normal behaviour. If you want to change this behaviour, you need to
do it in the MouseDown event:


public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
Button b = new Button(shell, SWT.PUSH);
final Text t = new Text(shell, SWT.BORDER);
t.setText("abcdefghijklmnop");

final int[] focusIn = new int[]{-1};

t.addListener(SWT.FocusIn, new Listener() {
public void handleEvent(Event e) {
System.out.println("Focus in "+e.time);
focusIn[0] = e.time;
t.selectAll(); // could get here via tabbing
}
});
t.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event e) {
System.out.println("Mouse Down "+e.time);
if (focusIn[0] == e.time) {
t.selectAll();
}
focusIn[0] = -1;
}
});
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}


"Ks" <friends4u20022002@yahoo.com> wrote in message
news:cj1m59$jib$1@eclipse.org...
>I have a peculiar problem while trying to get focus on a Text field using
> a mouse click, and was wondering if this was a Bug in SWT. Basically, when
> I get focus on a particular text field using a mouse click on that text
> field, I want to perform a selectAll() on it. This does not seem to work
> eventhough focusgained() is being invoked. However, the TAB key invokes
> focusgained() and also performs selectAll(). Here is the sample code for
> focusgained which is called on the listener using the Mouse click and TAB
> key when focus is gained.
>
> public void focusGained(FocusEvent e)
> {
> //focusGained() gets ivoked for the Mouse Click and Tab
>
> System.out.println("Focus gained");
>
> if(e.getSource() instanceof Text)
> {
> hrTxt.selectAll(); //Gets invoked for TAB and not Mouse click.
> }
> }
>
Previous Topic:Eclipse Plug-in SWT development in Voice arena
Next Topic:Ctrl+NumpadPlus resizes 'unresizable' columns
Goto Forum:
  


Current Time: Fri Sep 20 01:07:39 GMT 2024

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

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

Back to the top