Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » StyledText Extension
StyledText Extension [message #486336] Thu, 17 September 2009 10:36
Dirk Kitscha is currently offline Dirk KitschaFriend
Messages: 3
Registered: July 2009
Junior Member
I am evaluating SWT for a project. The main goal is to create a
"StyledTexteditor" with the ability to "tag" section of the text.
A tag is a combination of a name and a color. When you tag a section of
the text, you select some text and click on the "tag" button. Now you
get a "TagChooser", select your text and it will be tagged. That is
almost the same as the "set background color" action, but with the
ability that tags may overlap each other, i.e. a section of text can be
tagged with multiple tags.

The SWT StyledText component seems almost perfect for this purpose but I
don't see how to implement the tagging.
First I searched for a way to add new styles to the StyledText
component, but there seems to be no way, because the styles are hard coded.
Then I tried to use the "background color" and the data field for the
tagging purpose. The problem here is that the StyledText component
merges the styles (and only the last color "surives", which is
reasonable because one can only display one color at a time)

Text is: 1234567890

public class StyleTest1 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
StyledText text = new StyledText (shell, SWT.BORDER);

// make 0123456 have a red font
StyleRange style1 = new StyleRange();
style1.start = 0;
style1.length = 7; = "ExampleTag1";
style1.background = display.getSystemColor(SWT.COLOR_BLUE);
// make 34567890 have a red font
StyleRange style2 = new StyleRange();
style2.start = 3;
style2.length = 7; = "ExampleTag2";
style2.background = display.getSystemColor(SWT.COLOR_RED);

StringBuilder dumpText = new StringBuilder();
StyleRange[] styles = st.getStyleRanges();
if (styles.length > 0) {
for (int i = 0; i < styles.length; i++) {
if (styles[i].data != null) {
dumpText.append("[" + styles[i].toString() +
else {
dumpText.append("[" + styles[i].toString() + "]\n");


while (!shell.isDisposed()) {
if (!display.readAndDispatch())

[StyleRange {0, 3, fontStyle=normal, background=Color {0, 0,
[StyleRange {3, 7, fontStyle=normal, background=Color {255, 0,

What I need is something like:
[StyleRange {0, 3, fontStyle=normal, background=Color {0, 0, 255}]
[StyleRange {3, 4, fontStyle=normal, background=Color {128, 128,
128}(ExampleTag1,ExampleTag2)] // multi-tagged regions are displayed in
a special color (medium grey here)
[StyleRange {3, 7, fontStyle=normal, background=Color {255, 0, 0}]

or even better:
[StyleRange {0, 3, fontStyle=normal, tag=ExampleTag1]
[StyleRange {3, 4, fontStyle=normal, tag=ExampleTag1,ExampleTag2]
[StyleRange {3, 7, fontStyle=normal, tag=ExampleTag2]

Can anybody help me? Is there a way to do this task with the StyledText
component? Is there another/better way to do this task with SWT/JFace?

Thanks in advance!
Previous Topic:How to capture and produce IME composition Event?
Next Topic:Help with ITreePathContentProvider and ItreePathLabelProvider.
Goto Forum:

Current Time: Thu Jan 23 06:28:18 GMT 2020

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

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

Back to the top