Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Cursor position in SWT Text

Tags:

java

text

swt

I am trying to set the cursor position of SWT Text to the end after appending string. I am aware of changing cursor position of SWT StyledText, but not sure of SWT Text. If you people have come across this Challenge and found the solution kindly share it.

like image 362
Deepak Ramesh Avatar asked Dec 26 '12 10:12

Deepak Ramesh


1 Answers

Have a look at Text#setSelection(int):

public static void main(String[] args)
{
    Display d = new Display();
    final Shell shell = new Shell(d);
    shell.setLayout(new GridLayout(1, false));

    Text text = new Text(shell, SWT.BORDER);

    text.setText("Some random text here...");

    text.setSelection(text.getText().length());


    shell.pack();
    shell.open();
    while (!shell.isDisposed())
        while (!d.readAndDispatch())
            d.sleep();
}

This will set the cursor to the end.

like image 55
Baz Avatar answered Oct 07 '22 19:10

Baz