Home Tutorials Swt StyledText in SWT

Related Tutorials


 
 

Share on Google+Share on Google+

StyledText in SWT

Advertisement
To display the stylish and impressive text, we have used the class StyledText of package org.eclipse.swt.custom.

StyledText in SWT

     

This section illustrates you how to create a Styled Text.

To display the stylish and impressive text, we have used the class StyledText of package org.eclipse.swt.custom. The class StyleRange allows to create an array to hold the StyleRanges. We have create seven StyleRanges to display the text. The ranges[].underline is of Boolean type which underlines the styleRange. The ranges[].background sets the background color by using shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW). The ranges[3].fontStyle = SWT.BOLD sets the font of the StyleRange.

The method styledText.setFont(font) sets the font of the StyledText by using the class Font. The method styledText.replaceStyleRanges(0, 48, ranges) replace the style in the given range. 

Here is the code of StyledTextExample.java

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.*;
import org.eclipse.swt.graphics.Font;

public class StyledTextExample {
  Color orange,blue,red,green;
  
  public void run() {
  Display display = new Display();
  Shell shell = new Shell(display);
  
  orange = new Color(display, 255, 127, 0);
  blue = display.getSystemColor(SWT.COLOR_BLUE);
  red = display.getSystemColor(SWT.COLOR_RED);
  green = display.getSystemColor(SWT.COLOR_GREEN);

  create(shell);
  shell.setSize(430,100);
  shell.setText("Styled Text");
  shell.open();
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch()) {
  display.sleep();
  }
  }
  display.dispose();
  }
  private void create(final Shell shell) {
  shell.setLayout(new FillLayout());
  final StyledText styledText = new StyledText(shell, SWT.BORDER );
  styledText.setText("Java is an Object Oriented Programming Language.");
  Font font = new Font(shell.getDisplay(), "Courier", 10, SWT.NORMAL);
  styledText.setFont(font);

  StyleRange[] ranges = new StyleRange[7];
  ranges[0] = new StyleRange(0, 4, orange, null);
  ranges[0].underline = true;
ranges[0].background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
  ranges[0].fontStyle = SWT.BOLD;
  
  ranges[1] = new StyleRange(5, 2, blue, null);
ranges[1].background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);

  ranges[2] = new StyleRange(8, 2, red, null);
ranges[2].background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);

  ranges[3] = new StyleRange(11, 6, green, null);
  ranges[3].underline = true;
ranges[3].background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
  ranges[3].fontStyle = SWT.BOLD;

  ranges[4] = new StyleRange(18, 8, orange, null);
  ranges[4].underline = true;
ranges[4].background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
  ranges[4].fontStyle = SWT.BOLD;

  ranges[5] = new StyleRange(27, 11, blue, null);
ranges[5].background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
  
  ranges[6] = new StyleRange(39, 9, red, null);
ranges[6].background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
  styledText.replaceStyleRanges(0, 48, ranges);
  }
  public static void main(String[] args) {
  new StyledTextExample().run();
  }
}

 

Output will be displayed as:

Download Source Code

     

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 16, 2009

Related Tutorials

Discuss: StyledText in SWT  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com