Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » Caret reset on StyledText
Caret reset on StyledText [message #1730150] Thu, 21 April 2016 18:32
Mihael Schmidt is currently offline Mihael SchmidtFriend
Messages: 81
Registered: August 2010

I have a StyledText and want to set the caret to a new one on overwrite mode. So far so good. I registered a key listener and act upon SWT.INSERT and change the caret accordingly. But as soon as I start writing in overwrite mode the caret image is reset to the default one.

Any idea what could be wrong?

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Caret;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class CaretTest {

	public static boolean overwriteMode = false;
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setSize(200, 200);
		final StyledText textWidget = new StyledText(shell, SWT.BORDER);
		Caret caret = new Caret(textWidget, SWT.NONE);
		caret.setBounds(10, 10, 1, 16);
		textWidget.addKeyListener(new KeyListener() {
			public void keyReleased(KeyEvent arg0) {
			public void keyPressed(KeyEvent event) {
				if (event.keyCode == SWT.INSERT) {
					changeCaretPresentation(textWidget, isOverwriteMode());

			private void setOverwriteMode(boolean value) {
				overwriteMode = value;

			private boolean isOverwriteMode() {
				return overwriteMode;
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
	public static void changeCaretPresentation(StyledText textWidget, boolean overwriteMode) {
		Caret defaultCaret = textWidget.getCaret();
		GC graphicsContext = new GC(textWidget);
		FontMetrics fontMetrics = graphicsContext.getFontMetrics();
		if (overwriteMode) { //  block mode
			defaultCaret.setBounds(defaultCaret.getLocation().x, defaultCaret.getLocation().y, fontMetrics.getAverageCharWidth(), fontMetrics.getHeight());
		else { // beam mode
			defaultCaret.setBounds(defaultCaret.getLocation().x, defaultCaret.getLocation().y, 1, fontMetrics.getHeight());

Thanx in advance


[Updated on: Thu, 21 April 2016 18:40]

Report message to a moderator

Previous Topic:ViewPart tab edge (square vs soft)
Next Topic:WordWrap in Tabbed Property
Goto Forum:

Current Time: Mon May 10 08:46:22 GMT 2021

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

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

Back to the top