Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » StyledText setCaretOffset not working?
StyledText setCaretOffset not working? [message #467516] Wed, 01 February 2006 12:54 Go to next message
Eclipse UserFriend
Originally posted by: eclipse.insomnia-consulting.org

I'm writing code that replaces text in a StyledText widget during the
TextChanged event.

The code that's giving me problems is...

StyledText sv = getSourceViewer().getTextWidget() ;
int offset = sv.getCaretOffset() ;


sv.setSelection(0, offset) ;
ignore = true ; //to ensure the textChanged doesn't get executed a second
time

sv.insert("//test\nnop|nop |nop|nop|nop\nnop|clracc|") ;
ignore = false ;

sv.setCaretOffset(0);

I'd expect, at the end of this the caret would be at the beginning of the
text. However, it is always at the original caretOffset position.

Any thoughts? (Eclipse 3.1.1 on Mac OSX 10.4)
Re: StyledText setCaretOffset not working? [message #467518 is a reply to message #467516] Wed, 01 February 2006 13:07 Go to previous messageGo to next message
Stefan Langer is currently offline Stefan LangerFriend
Messages: 236
Registered: July 2009
Senior Member
I had a similar problem and solved it by executing the setCaretOffset in
asyncExec. Thus it gets executed after my eventhandling code is done.
This seems to work although I can't guarantee that this is crossplatform
compatible since I only execute the code on windows.

Regards
Stefan

greg akins wrote:
> I'm writing code that replaces text in a StyledText widget during the
> TextChanged event.
>
> The code that's giving me problems is...
>
> StyledText sv = getSourceViewer().getTextWidget() ;
> int offset = sv.getCaretOffset() ;
>
> sv.setSelection(0, offset) ;
> ignore = true ; //to ensure the textChanged doesn't get executed a
> second time
>
> sv.insert("//test\nnop|nop |nop|nop|nop\nnop|clracc|") ; ignore =
> false ;
> sv.setCaretOffset(0);
>
> I'd expect, at the end of this the caret would be at the beginning of
> the text. However, it is always at the original caretOffset position.
>
> Any thoughts? (Eclipse 3.1.1 on Mac OSX 10.4)
>
>
Re: StyledText setCaretOffset not working? [message #467522 is a reply to message #467518] Wed, 01 February 2006 14:35 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: eclipse.insomnia-consulting.org

Stefan Langer wrote:

> I had a similar problem and solved it by executing the setCaretOffset in
> asyncExec. Thus it gets executed after my eventhandling code is done.
> This seems to work although I can't guarantee that this is crossplatform
> compatible since I only execute the code on windows.

> Regards
> Stefan

Thanks Stefan... I'm not familiar with asyncExec? Can you give me an
example?
Re: StyledText setCaretOffset not working? [message #467526 is a reply to message #467516] Wed, 01 February 2006 15:18 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

greg akins <eclipse@insomnia-consulting.org> wrote:
> I'm writing code that replaces text in a StyledText widget during the
> TextChanged event.
>
> The code that's giving me problems is...
>
> StyledText sv = getSourceViewer().getTextWidget() ;
> int offset = sv.getCaretOffset() ;
>
>
> sv.setSelection(0, offset) ;
> ignore = true ; //to ensure the textChanged doesn't get executed a
> second time
>
> sv.insert("//test\nnop|nop |nop|nop|nop\nnop|clracc|") ;
> ignore = false ;
>
> sv.setCaretOffset(0);
>
> I'd expect, at the end of this the caret would be at the beginning of
> the text. However, it is always at the original caretOffset position.
>
try:
sv.setSelection(0,0)

--
Sunil
Re: StyledText setCaretOffset not working? [message #467531 is a reply to message #467526] Wed, 01 February 2006 15:59 Go to previous messageGo to next message
Greg Akins is currently offline Greg AkinsFriend
Messages: 4
Registered: July 2009
Junior Member
The asyncExec worked.

I had tried setSelection, and that didn't work either.

Thanks for the suggestions.

-greg
Re: StyledText setCaretOffset not working? [message #467586 is a reply to message #467522] Wed, 01 February 2006 16:02 Go to previous message
Stefan Langer is currently offline Stefan LangerFriend
Messages: 236
Registered: July 2009
Senior Member
This is a snippet taken out from my program.

Display.getCurrent().asyncExec(new Runnable(){
public void run()
{
if(!comp_.isDisposed())
comp_.setCaretOffset(0);
}
};

comp_ is the Composite I'm setting the caretposition on. It is passed to
the runnable as a final variable.

The comp.isDisposed is there to check if the composite hasn't been
disposed by the time my code gets executed.

There is one more thing make sure you execute this code in the UI-Thread
or else Display.getCurrent() will return null and a NPException will be
thrown. I use an assertion to check for this condition.

Regards
Stefan

greg akins wrote:
> Stefan Langer wrote:
>
>> I had a similar problem and solved it by executing the setCaretOffset
>> in asyncExec. Thus it gets executed after my eventhandling code is
>> done. This seems to work although I can't guarantee that this is
>> crossplatform compatible since I only execute the code on windows.
>
>> Regards
>> Stefan
>
> Thanks Stefan... I'm not familiar with asyncExec? Can you give me an
> example?
>
Previous Topic:Dialog Resizing
Next Topic:Switching off BIDI mode for StyledText control
Goto Forum:
  


Current Time: Thu Mar 28 22:12:38 GMT 2024

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

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

Back to the top