LineBackgroundEvent Fields
FieldDescription
int lineOffsetThe zero-based offset, relative to the whole text, of the line the StyledText needs background color information for. Note: this is the character offset, not the line number.
String lineTextThe text of the line the StyledText needs background color information for.
Color lineBackgroundThe field that holds the color you set. The StyledText uses this field to set the background color for the line.
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.LineBackgroundEvent;
import org.eclipse.swt.custom.LineBackgroundListener;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class StyledTextLineBackgroundListener {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
final StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
styledText.setText("\n1234\n124\n\1234\n12314\n\1241234\n");
styledText.addLineBackgroundListener(new LineBackgroundListener() {
public void lineGetBackground(LineBackgroundEvent event) {
if (event.lineText.indexOf("SWT") > -1) {
event.lineBackground = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
}
}
});
styledText.setBounds(10, 10, 500, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}