Community
Participate
Working Groups
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(); } }
(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()));
(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?