Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » How to set caretPosition in a Text on Linux
How to set caretPosition in a Text on Linux [message #436479] Fri, 09 September 2005 08:29 Go to next message
sabina is currently offline sabinaFriend
Messages: 1
Registered: July 2009
Junior Member
In my application I need to change user input in a text field and set
caret position accordingly to the new entered text. I've written a sample
that works fine on Eclipse 3.0.2 on Windows but doesn't in in Linux. (Text
is changed but caret position is not where I expect, that is where I put
it with setSelection(..) ). Can anyone help?

Thank you in advance
Sabina - Italy

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Tester {

static boolean formatting = false;
public static void main(String[] args) {

Display display = new Display ();
Shell shell = new Shell (display);
final Text text = new Text (shell, SWT.BORDER );
text.setBounds (10, 10, 100, 25);
text.addVerifyListener(new VerifyListener() {

public void verifyText(VerifyEvent e) {
String s = e.text;
System.out.println( text.getCaretPosition() +
"-" + text.getCaretLocation().y +
"-" + text.getCaretLocation().x );
if(s.trim().equals("1")){
formatting = true;
text.setText("Change");
text.setSelection(6,6);
System.out.println( text.getCaretPosition() +
"-" + text.getCaretLocation().y +
"-" + text.getCaretLocation().x );
formatting = false;
e.doit = false;
}

}});

text.addModifyListener( new ModifyListener() {
public void modifyText(ModifyEvent e) {
if (formatting)
return;
}
});
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Re: How to set caretPosition in a Text on Linux [message #436496 is a reply to message #436479] Fri, 09 September 2005 14:58 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi Sabina,

You're seeing an swt-gtk bug in 3.0.2 that is fixed in 3.1. So you can
either use 3.1 instead, or if this is not an option, you can use a
StyledText instead of a Text. StyledText offers a setCaretOffset(int) api.

Grant

"sabina" <sabinal@iol.it> wrote in message
news:373443b8d0f020c977e0613414b19656$1@www.eclipse.org...
> In my application I need to change user input in a text field and set
> caret position accordingly to the new entered text. I've written a sample
> that works fine on Eclipse 3.0.2 on Windows but doesn't in in Linux. (Text
> is changed but caret position is not where I expect, that is where I put
> it with setSelection(..) ). Can anyone help?
>
> Thank you in advance
> Sabina - Italy
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.ModifyEvent;
> import org.eclipse.swt.events.ModifyListener;
> import org.eclipse.swt.events.VerifyEvent;
> import org.eclipse.swt.events.VerifyListener;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Text;
>
> public class Tester {
>
> static boolean formatting = false;
> public static void main(String[] args) {
>
> Display display = new Display ();
> Shell shell = new Shell (display);
> final Text text = new Text (shell, SWT.BORDER );
> text.setBounds (10, 10, 100, 25);
> text.addVerifyListener(new VerifyListener() {
>
> public void verifyText(VerifyEvent e) {
> String s = e.text;
> System.out.println( text.getCaretPosition() +
> "-" + text.getCaretLocation().y +
> "-" + text.getCaretLocation().x );
> if(s.trim().equals("1")){
> formatting = true;
> text.setText("Change");
> text.setSelection(6,6);
> System.out.println( text.getCaretPosition() +
> "-" + text.getCaretLocation().y +
> "-" + text.getCaretLocation().x );
> formatting = false;
> e.doit = false;
> }
>
> }});
>
> text.addModifyListener( new ModifyListener() {
> public void modifyText(ModifyEvent e) {
> if (formatting)
> return;
> }
> });
> shell.open ();
> while (!shell.isDisposed ()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> display.dispose ();
> }
> }
>
Previous Topic:Classloader problems
Next Topic:How to enable/disable Actions?
Goto Forum:
  


Current Time: Sun Dec 08 15:33:30 GMT 2024

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

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

Back to the top