StringPrint.java Source code

Java tutorial

Introduction

Here is the source code for StringPrint.java

Source

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class StringPrint {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.open();
        PrinterData data = Printer.getDefaultPrinterData();
        if (data == null) {
            System.out.println("Warning: No default printer.");
        }
        Printer printer = new Printer(data);
        if (printer.startJob("SWT Printing Snippet")) {
            Color black = printer.getSystemColor(SWT.COLOR_BLACK);
            GC gc = new GC(printer);
            if (printer.startPage()) {
                gc.setForeground(black);
                String testString = "Hello World!";
                gc.drawString(testString, 200, 200);
                printer.endPage();
            }
            gc.dispose();
            printer.endJob();
        }
        printer.dispose();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}