By
using PrintDialog widget, user can select a printer from various available
printers in the system.
Ex:
PrintDialog
printDialog = new PrintDialog(shell, SWT.NONE);
printDialog.setText("PrintDialogDemo");
printDialog.setScope(PrinterData.PAGE_RANGE);
printDialog.setStartPage(2);
printDialog.setEndPage(5);
printDialog.setPrintToFile(true);
PrinterData
printerData = printDialog.open();
Following
is the complete working application.
import java.util.Objects; import org.eclipse.swt.SWT; 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 Test { private static void addWidgetsToShell(Display display, Shell shell) { PrintDialog printDialog = new PrintDialog(shell, SWT.NONE); printDialog.setText("PrintDialogDemo"); printDialog.setScope(PrinterData.PAGE_RANGE); printDialog.setStartPage(2); printDialog.setEndPage(5); printDialog.setPrintToFile(true); PrinterData printerData = printDialog.open(); if (!Objects.isNull(printerData)) { switch (printerData.scope) { case PrinterData.ALL_PAGES: System.out.println("Printing all pages."); break; case PrinterData.SELECTION: System.out.println("Printing selected page."); break; case PrinterData.PAGE_RANGE: System.out.print("Printing page range. "); System.out.print("From:" + printerData.startPage); System.out.println(" to:" + printerData.endPage); break; } if (printerData.printToFile) System.out.println("Printing to file."); else System.out.println("Not printing to file."); if (printerData.collate) System.out.println("Collating."); else System.out.println("Not collating."); System.out.println("Number of copies:" + printerData.copyCount); System.out.println("Printer Name:" + printerData.name); } shell.dispose(); } public static void main(String[] args) { /* Instantiate Display object, it represents SWT session */ Display display = new Display(); /* * Define Shell, it represent a window, You can add more than one shell * to Display */ Shell shell = new Shell(display); shell.setSize(500, 500); addWidgetsToShell(display, shell); /* * Run the event dispatching loop until an exit condition occurs, which * is typically when the main shell window is closed by the user. */ while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } /* Dispose the display */ display.dispose(); } }
No comments:
Post a Comment