Select all content of a text widget after a mouse click [message #1857731] |
Thu, 23 February 2023 08:25 |
Hélène O. Messages: 4 Registered: December 2021 |
Junior Member |
|
|
Hi,
I add a FocusListener on my Text widgets to select its content on focus gained.
It works fine. But I want the same behaviour if I click inside the Text widget.
For the moment, if I click inside, it displays a blinking cursor where the click occurs.
Here is a little snippet showing the problem:
package test;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
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 DemoSelectAllText {
public static void main (String [] args) {
DemoSelectAllText me = new DemoSelectAllText();
me.run();
}
private void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2, true));
Text text = new Text(shell, 0);
text.setText("First");
text.setLayoutData(new GridData());
Text text2 = new Text(shell, 0);
text2.setText("Second");
text2.setLayoutData(new GridData());
text.addFocusListener(new MyFocusListener(text));
text2.addFocusListener(new MyFocusListener(text2));
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
public class MyFocusListener extends FocusAdapter {
private Text text;
public MyFocusListener(Text text) {
this.text = text;
}
@Override
public void focusLost(FocusEvent arg0) {
}
@Override
public void focusGained(FocusEvent arg0) {
text.selectAll();
}
}
}
Thanks!
|
|
|
Powered by
FUDForum. Page generated in 0.03344 seconds