0

How to retain multiple occurrences of same string get selected within same styled text content? Single occurrence can be selected using setSelection(). Is there any similar options?

Baz
  • 36,440
  • 11
  • 68
  • 94
Sana
  • 118
  • 2
  • 13

1 Answers1

1

Use StyleRange to set multiple occurrences of the string.

Snippet:

    String searchKey = "hello";
    String content = styledText.getText(); // StyledText instance
    int index = content.indexOf(searchKey, 0);
    do {
        if (index != -1) {
            StyleRange styleRange = new StyleRange(index, searchKey.length(), Display.getCurrent().getSystemColor(
                    SWT.COLOR_BLACK), Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW));
            styledText.setStyleRange(styleRange);
            index = content.indexOf(searchKey, index + 1);
        } else {
            System.out.println("End of search");
            break;
        }

    } while (index != -1);

Refer this article an examples here on style ranges.

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
  • Thanks for your valuable suggestion.But I want to just set all occurance of a string got selected.I have already implemented the case that you have explained above. – Sana May 13 '14 at 11:52
  • 1
    No its not possible. `In any text editor you can select only ONE RANGE of text(From character index n to character index n+/-x) not multiple at a given time`. But you can highlight the search terms in editors by changing the background and foreground colour. – Chandrayya G K May 13 '14 at 12:01