Bug 569383

Summary: [WIN32] FocusIn listener on Text field does not select all text with selectAll call
Product: [Eclipse Project] Platform Reporter: Lars Vogel <Lars.Vogel>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: Lars.Vogel, niraj.modi
Version: 4.17   
Target Milestone: ---   
Hardware: PC   
OS: Windows 10   
See Also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=46059
Whiteboard:

Description Lars Vogel CLA 2020-12-02 04:20:48 EST
I would like a text widget that selects all of its text when it gains focus. 
This doesn't appear to be currently supported by SWT.

1.) Run snippet
2.) Select left text box with the mouse.


OBSERVED RESULTS:
Selection expands to all of the text, and then reduces to just the cursor.


DESIRED RESULTS:
Selection expands to all of the text, and stays that way.



I have to wrap it into a display.asyncExec(() -> text.setSelection(0, text.getText().length()))

This seems to be the same issue as Bug 46059 which got marked as fixed but it still does not work.

----

/*
 * Text example snippet: implement content assist
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Snippet320 {

	private static Text text;

public static void main(String [] args) {
	final Display display = new Display();
	final Shell shell = new Shell(display);
	shell.setText("Snippet 320");
	shell.setLayout(new GridLayout());
	Text local = new Text(shell, SWT.SINGLE | SWT.BORDER);
	text = new Text(shell, SWT.SINGLE | SWT.BORDER);
	// This does not work
	text.addListener(SWT.FocusIn, event -> text.selectAll());
	// This does work
//	text.addListener(SWT.FocusIn, event -> display.asyncExec(() -> text.setSelection(0, text.getText().length())));
	text.setLayoutData(new GridData(150, SWT.DEFAULT));
	shell.pack();
	shell.open();



	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
	display.dispose();
}

}
Comment 1 Niraj Modi CLA 2020-12-04 03:16:05 EST
(In reply to Lars Vogel from comment #0)
> I would like a text widget that selects all of its text when it gains focus. 
> This doesn't appear to be currently supported by SWT.
> 	// This does not work
> 	text.addListener(SWT.FocusIn, event -> text.selectAll());

Hi Lars,
Got the snippet working by wrapping 'selectAll()' into a display.asyncExec() as below:
text.addListener(SWT.FocusIn, event -> display.asyncExec(() -> text.selectAll()));
Comment 2 Lars Vogel CLA 2020-12-04 03:24:13 EST
(In reply to Niraj Modi from comment #1)
> (In reply to Lars Vogel from comment #0)
> > I would like a text widget that selects all of its text when it gains focus. 
> > This doesn't appear to be currently supported by SWT.
> > 	// This does not work
> > 	text.addListener(SWT.FocusIn, event -> text.selectAll());
> 
> Hi Lars,
> Got the snippet working by wrapping 'selectAll()' into a display.asyncExec()
> as below:
> text.addListener(SWT.FocusIn, event -> display.asyncExec(() ->
> text.selectAll()));

Sounds like my workaround from comment 0 (I have to wrap it into a display.asyncExec(() -> text.setSelection(0, text.getText().length())))

But IMHO this should not be necessary and we have a bug in SWT. WDYT?