Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Select all content of a text widget after a mouse click(How can I select all the content of a Text widget after a mouse click)
Select all content of a text widget after a mouse click [message #1857731] Thu, 23 February 2023 08:25
Hélène O. is currently offline Hélène O.Friend
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!
Previous Topic:Scrolling Issue with ScrolledFormText
Next Topic:SWT_AWT DPI scaling
Goto Forum:
  


Current Time: Mon May 06 13:17:49 GMT 2024

Powered by FUDForum. Page generated in 0.02490 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top