Styled Text with highlighted Odd Line : StyledText « SWT JFace Eclipse « Java

Styled Text with highlighted Odd Line

Styled Text with highlighted Odd Line
 * All Right Reserved. 
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * Created on Feb 22, 2004 1:29:05 AM by JACK
 * $Id$
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class HighlightOddLine {
  Display display = new Display();
  Shell shell = new Shell(display);
  StyledText styledText;
  public HighlightOddLine() {
    shell.setLayout(new GridLayout());
    styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
    styledText.addLineBackgroundListener(new LineBackgroundListener() {
      public void lineGetBackground(LineBackgroundEvent event) {
        if(styledText.getLineAtOffset(event.lineOffset) % 2 == 1)
          event.lineBackground = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styledText.setText("Line 0\r\nLine 1\r\nLine 2\r\nLine 3\r\nLine 4\r\nLine 5\r\nLine 6");

    shell.setSize(300, 150);;

    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue


  private void init() {


  public static void main(String[] args) {
    new HighlightOddLine();


Related examples in the same category

1.Sample Styled TextSample Styled Text
2.Search Style TextSearch Style Text
3.SetLine Background SetLine Background
4.Demonstrates StyleRangesDemonstrates StyleRanges
5.Implements syntax coloring using the StyledText APIImplements syntax coloring using the StyledText API
6.SWT StyledText
7.Text with underline and strike throughText with underline and strike through
8.Setting the font style, foreground and background colors of StyledTextSetting the font style, foreground and background colors of StyledText