Example usage for org.eclipse.swt.custom StyledText setFont

List of usage examples for org.eclipse.swt.custom StyledText setFont

Introduction

In this page you can find the example usage for org.eclipse.swt.custom StyledText setFont.

Prototype

public void setFont(Font font) 

Source Link

Document

Sets a new font to render text with.

Usage

From source file:org.eclipse.swt.snippets.Snippet218.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 218");
    shell.setLayout(new FillLayout());
    final StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setText(text);/*w  w  w.  j  av a2 s  . c  o m*/
    FontData data = display.getSystemFont().getFontData()[0];
    Font font = new Font(display, data.getName(), 16, SWT.BOLD);
    styledText.setFont(font);
    styledText.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
    styledText.addListener(SWT.Resize, event -> {
        Rectangle rect = styledText.getClientArea();
        Image newImage = new Image(display, 1, Math.max(1, rect.height));
        GC gc = new GC(newImage);
        gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
        gc.fillGradientRectangle(rect.x, rect.y, 1, rect.height, true);
        gc.dispose();
        styledText.setBackgroundImage(newImage);
        if (oldImage != null)
            oldImage.dispose();
        oldImage = newImage;
    });
    shell.setSize(700, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (oldImage != null)
        oldImage.dispose();
    font.dispose();
    display.dispose();
}

From source file:StyledTextGradientBackground.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setText(text);//ww w  .j a  v  a 2s  .  com
    FontData data = display.getSystemFont().getFontData()[0];
    Font font = new Font(display, data.getName(), 16, SWT.BOLD);
    styledText.setFont(font);
    styledText.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
    styledText.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event event) {
            Rectangle rect = styledText.getClientArea();
            Image newImage = new Image(display, 1, Math.max(1, rect.height));
            GC gc = new GC(newImage);
            gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
            gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
            gc.fillGradientRectangle(rect.x, rect.y, 1, rect.height, true);
            gc.dispose();
            styledText.setBackgroundImage(newImage);
            if (oldImage != null)
                oldImage.dispose();
            oldImage = newImage;
        }
    });
    shell.setSize(700, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    if (oldImage != null)
        oldImage.dispose();
    font.dispose();
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet332.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 332");
    GridLayout layout = new GridLayout();
    layout.marginHeight = layout.marginWidth = 10;
    shell.setLayout(layout);//ww  w.j a va  2s . c  o  m
    StyledText text = new StyledText(shell, SWT.MULTI | SWT.BORDER);
    final String segment = "Eclipse";
    String string = "Force RTL direction on this segment \"" + segment + "\".";
    text.setText(string);
    int[] segments = { string.indexOf(segment), segment.length() };
    StyleRange[] ranges = { new StyleRange(0, 0, display.getSystemColor(SWT.COLOR_RED), null) };
    text.setStyleRanges(segments, ranges);
    Font font = new Font(display, "Tahoma", 16, 0);
    text.setFont(font);
    text.addBidiSegmentListener(event -> {
        String string1 = event.lineText;
        int start = string1.indexOf(segment);
        event.segments = new int[] { start, start + segment.length() };
        event.segmentsChars = new char[] { '\u202e', '\u202C' };
    });
    Combo combo = new Combo(shell, SWT.SIMPLE);
    combo.setFont(font);
    combo.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
    combo.setItems("Option 1...", "Option 2...", "Option 3...", "Option 4...");
    combo.select(1);
    combo.addSegmentListener(event -> {
        event.segments = new int[] { 0, event.lineText.length() };
        event.segmentsChars = new char[] { '\u202e', '\u202c' };
    });
    shell.setSize(500, 250);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    font.dispose();
    display.dispose();
}