Java Tutorial/SWT/PrintDialog

Материал из Java эксперт
Перейти к: навигация, поиск

17. PrintDialog.open() returns a PrinterData object.

import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    PrintDialog dlg = new PrintDialog(shell);
    PrinterData printerData = dlg.open();
    if (printerData != null) {
      System.out.println(printerData.printToFile);
      // Printer printer = new Printer(printerData);
      // Use printer . . .
      // printer.dispose();
    }
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





17. Select some options programmatically before displaying the dialog

import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    PrintDialog dlg = new PrintDialog(shell);
    
    dlg.setScope(PrinterData.SELECTION);
    
    PrinterData printerData = dlg.open();
    if (printerData != null) {
      System.out.println(printerData.printToFile);
      // Printer printer = new Printer(printerData);
      // Use printer . . .
      // printer.dispose();
    }
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}