Hi All,
Can someone help me how to print a composite and hence utilize
the layout capabilities in swt?
In the printing examples I’ve looked at, a GC is
created and all “layout” is handled manually. I have a
number of charts
which are layout using GridLayout which I would like
to print as they appear on screen.
I tried the following code (see below) but got a Thread Access
Error when creating the Display. The Action is a simple
menu action
called within the UI thread.
I would appreciate any help or hints! Thanks!
Jorgen E.
ChartWindow chartWindow = (ChartWindow)window;
int nbrPages
= chartWindow.getNbrTabs();
PrintDialog dialog = new PrintDialog(window.getShell(), SWT.NONE);
dialog.setText("PrintDialogDemo");
dialog.setScope(PrinterData.PAGE_RANGE);
dialog.setStartPage(1);
dialog.setEndPage(nbrPages);
dialog.setPrintToFile(true);
PrinterData printerData = dialog.open();
if (printerData != null) {
Printer
printer = new Printer(printerData);
//GC
gc = new GC(printer);
DeviceData deviceData = printer.getDeviceData();
Display
display = new Display(deviceData);
printer.startJob("Chart");
printer.startPage();
Shell
shell = new Shell(display);
ChartResources chartResources =
new ChartResources(shell);
int itemId[]
= chartWindow.getItemId();
GridLayout layout = new GridLayout();
layout.numColumns = 3;
layout.makeColumnsEqualWidth = true;
shell.setLayout(layout);
for (int i
= 0; i < ItemId.length; i++) {
Chart
chart = ChartKit.getChart(itemId[i],
false, env);
ChartCanvas canvas = new ChartCanvas(chart, chartResources, shell, SWT.BORDER);
canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
}
printer.endPage();
printer.endJob();
printer.dispose();
}
}
org.eclipse.swt.SWTException: Invalid thread
access
at org.eclipse.swt.SWT.error(SWT.java:2691)
at org.eclipse.swt.SWT.error(SWT.java:2616)
at org.eclipse.swt.SWT.error(SWT.java:2587)
at org.eclipse.swt.widgets.Display.checkDisplay(Display.java:568)
at org.eclipse.swt.widgets.Display.create(Display.java:629)
at org.eclipse.swt.graphics.Device.<init>(Device.java:99)
at org.eclipse.swt.widgets.Display.<init>(Display.java:355)
at com.finsys.tradeagent.chart.PrintAction.run(PrintAction.java:63)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:881)
at
org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:915)
at
org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:866)
at
org.eclipse.jface.action.ActionContributionItem$7.handleEvent(ActionContributionItem.java:785)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:82)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:796)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:2772)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2431)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:668)
at org.eclipse.jface.window.Window.open(Window.java:648)
at com.finsys.tradeagent.TradeAgentWindow.main(TradeAgentWindow.java:132)