1

I need to create a SWT StyledText Control which shows the human readable characters with equal spacing for each character as we see in a "notepad".

But when I create a Text


    StyledText outputText = new StyledText(scrolledComposite_2, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    scrolledComposite_2.setContent(outputText);
    scrolledComposite_2.setMinSize(outputText.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    outputText.setText(data.toString());
    outputText.setStyleRange(new StyleRange(0, data.toString().length(), CMFUtils.GREEN, CMFUtils.WHITE));

When the StyledText is displayed in displays the data, but the width of each character is different. Say for example, the width of 'i' or ' ' (space) is smaller than other characters like 'S', which causes me problems while pointing the errored character using '^' in the next line. For example:

output>>>>
blah-blah-blah-sdlfk-blah
................^

As you can see, the error in the above sample string occurs at "sdlfk" which is at 16th character and when I try to point it by putting a '^' in the next line with 16 spaces (note: the periods in this example will be replaced by spaces in my original output), it points to a different position in the string.

Baz
  • 36,440
  • 11
  • 68
  • 94
Parasu
  • 192
  • 2
  • 13

1 Answers1

2

Use a monospaced SWT font.

Font font = new Font(display, "Monospaced", 12, SWT.NORMAL);
outputText.setFont(font);
// ...
// In SWT: if you created it, you dispose it.
font.dispose();
sina72
  • 4,931
  • 3
  • 35
  • 36
  • 1
    But please remember to `dispose()` of the `Font`. Otherwise you will create a memory leak. – Baz Jun 23 '14 at 08:29
  • Thanks for your answer. But unfortunately SWT is not taking it and still displays the text without mono spacing. [One of the stackoverflow answers](http://stackoverflow.com/questions/221568/swt-os-agnostic-way-to-get-monospaced-font) has a pointer to this bug [48055](https://bugs.eclipse.org/bugs/show_bug.cgi?id=48055) which describes this behavior. Since my SWT application always runs on windows, I am planning to use "Courier New" as the monospaced font. – Parasu Jun 23 '14 at 13:28
  • 1
    @Parasu Somebody in that bug report posted a "Cross platform, thread-safe, monospaced font utility class". Might be worth using that instead of "just" courier. – Baz Jun 23 '14 at 15:34
  • @Baz yes, I didn't see that class. That is very useful. Thanks for pointing me. – Parasu Jul 30 '14 at 04:08