How to retain multiple occurrences of same string get selected within same styled text content?

StackOverflow https://stackoverflow.com/questions/23624053

  •  21-07-2023
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top