More Tutorials| Bioinformatics| Open Source| Photoshop| Questions? | Software Development
 

StyledText in SWT

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

                         

» View all related tutorials
Related Tags: c class list orm text select form console button io swt size method sed display sets toolbar ole this location

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.