Bug 569383 - [WIN32] FocusIn listener on Text field does not select all text with selectAll call
Summary: [WIN32] FocusIn listener on Text field does not select all text with selectAl...
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.17   Edit
Hardware: PC Windows 10
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-02 04:20 EST by Lars Vogel CLA
Modified: 2020-12-04 03:24 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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?