Demonstrates StyleRanges : StyledText « SWT JFace Eclipse « Java






Demonstrates StyleRanges

Demonstrates StyleRanges
//Send questions, comments, bug reports, etc. to the authors:

//Rob Warner (rwarner@interspatial.com)
//Robert Harris (rbrt_harris@yahoo.com)

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * This class demonstrates StyleRanges
 */
public class StyleRangeTest {
  private Color orange;
  private Color blue;

  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);

    // Create colors for style ranges
    orange = new Color(display, 255, 127, 0);
    blue = display.getSystemColor(SWT.COLOR_BLUE);

    createContents(shell);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }

    // We created orange, but not blue
    orange.dispose();

    display.dispose();
  }

  /**
   * Creates the main window contents
   * 
   * @param shell the main window
   */
  private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());

    // Create the StyledText
    StyledText styledText = new StyledText(shell, SWT.BORDER);

    // Set the text
    styledText.setText("Go Gators");

    /*
     * The multiple setStyleRange() method // Turn all of the text orange, with
     * the default background color styledText.setStyleRange(new StyleRange(0, 9,
     * orange, null));
     *  // Turn "Gators" blue styledText.setStyleRange(new StyleRange(3, 6, blue,
     * null));
     */

    /*
     * The setStyleRanges() method // Create the array to hold the StyleRanges
     * StyleRange[] ranges = new StyleRange[2];
     *  // Create the first StyleRange, making sure not to overlap. Include the
     * space. ranges[0] = new StyleRange(0, 3, orange, null);
     *  // Create the second StyleRange ranges[1] = new StyleRange(3, 6, blue,
     * null);
     *  // Replace all the StyleRanges for the StyledText
     * styledText.setStyleRanges(ranges);
     */

    /* The replaceStyleRanges() method */
    // Create the array to hold the StyleRanges
    StyleRange[] ranges = new StyleRange[2];

    // Create the first StyleRange, making sure not to overlap. Include the
    // space.
    ranges[0] = new StyleRange(0, 3, orange, null);

    // Create the second StyleRange
    ranges[1] = new StyleRange(3, 6, blue, null);

    // Replace only the StyleRanges in the affected area
    styledText.replaceStyleRanges(0, 9, ranges);
  }

  /**
   * The application entry point
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new StyleRangeTest().run();
  }
}



           
       








Related examples in the same category

1.Sample Styled TextSample Styled Text
2.Styled Text with highlighted Odd LineStyled Text with highlighted Odd Line
3.Search Style TextSearch Style Text
4.SetLine Background SetLine Background
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