selectAll( ) does not work on Text when the widget gets focus with Mouse click. [message #443457] |
Fri, 24 September 2004 17:39 |
Eclipse User |
|
|
|
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 #443709 is a reply to message #443457] |
Wed, 29 September 2004 15:04 |
Veronika Irvine 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.
> }
> }
>
|
|
|
Powered by
FUDForum. Page generated in 0.03212 seconds