Sample Styled Text : StyledText « SWT JFace Eclipse « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » SWT JFace Eclipse » StyledText 




Sample Styled Text
Sample Styled Text


/******************************************************************************
 * All Right Reserved. 
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 
 * Created on Feb 19, 2004 8:49:16 PM by JACK
 * $Id$
 
 *****************************************************************************/

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
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 SampleStyledText {
  Display display = new Display();
  Shell shell = new Shell(display);
  
  StyledText styledText;

  public SampleStyledText() {
    init();
    
    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));
    
    Font font = new Font(shell.getDisplay()"Courier New"12, SWT.NORMAL);
    styledText.setFont(font);
    
    styledText.setText("123456789\r\nABCDEFGHI");
    
    StyleRange styleRange1 = new StyleRange();
    styleRange1.start = 2;
    styleRange1.length = 16;
    styleRange1.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    styleRange1.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange1.fontStyle = SWT.BOLD;
    
    StyleRange styleRange2 = new StyleRange();
    styleRange2.start = 14;
    styleRange2.length = 3;
    styleRange2.fontStyle = SWT.NORMAL;
    styleRange2.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
    styleRange2.background = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    
//    styledText.setStyleRange(styleRange1);
//    styledText.setStyleRange(styleRange2);
    
    //styledText.setStyleRanges(new StyleRange[]{styleRange1, styleRange2});
    //styledText.setStyleRanges(new StyleRange[]{styleRange2, styleRange1});
    
    //styledText.setLineBackground(1, 1, shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
    
//    styledText.setSelection(4);
//    System.out.println(printStyleRanges(styledText.getStyleRanges()) );
//    styledText.insert("000");
    
    
    System.out.println(printStyleRanges(styledText.getStyleRanges()) );
    
//    styledText.setStyleRanges(new StyleRange[]{styleRange1});
//    System.out.println(printStyleRanges(styledText.getStyleRanges()) );
    
    //shell.pack();
    shell.setSize(300120);
    shell.open();
    //textUser.forceFocus();

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

    display.dispose();
  }
  
  private String printStyleRanges(StyleRange[] styleRanges) {
    
    if(styleRanges == null)
      return "null";
    else if(styleRanges.length == 0)
      return "[]";
    
    StringBuffer sb = new StringBuffer();
    for(int i=0; i<styleRanges.length; i++) {
      sb.append(styleRanges[i"\n");
    }
    
    return sb.toString();
  }

  private void init() {

  }

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


           
       














Related examples in the same category
1.Styled Text with highlighted Odd LineStyled Text with highlighted Odd Line
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.