The following example uses the printPage
method to print a range of table cells
that a user has selected. This example assumes that you have a table named myTable
.
This example prints the cells to as many physical pages as is necessary.
// create printer TablePrinter printer = new TablePrinter(myTable); // get a print jobFrame frame = oracle.bali.ewt.util.WindowUtils.parentFrame(myTable); PrintJob job = (java.awt.Toolkit.getDefaultToolkit()).getPrintJob(frame, "Printer", null); // set the page size, adjusting the Dimension for 1/2-inch margins all around Dimension pageDim = job.getPageDimension(); pageDim.height = pageDim.height - 72; pageDim.width = pageDim.width - 72; printer.setViewDimension(pageDim); // set scale for the table, if you want to printer.setPrintScaleType(TablePrinter.SCALE_TO_ZOOM_FACTOR); printer.setPrintZoomFactor(75); // find the rows and columns that the user has selected DataAccess dataAccess = myTable.getTableModel().getDataAccess(); // first, find current page int[] hPosCurrent = dataAccess.getEdgeCurrentHPos(oracle.dss.util.DataDirector.PAGE_EDGE); // get a DataComponentHandle; in real code, you would have something // more sophisticated ComponentHandle compHandle = myTable.getSelectedComponent(); if (!(compHandle instanceof DataRangeComponentHandle)) { // get out return; // assuming this is in a method that returns void } DataComponentInfo[] cellRange = compHandle.getLocationList(); int firstSelectedRow = cellRange[0].getRow(); int lastSelectedRow = cellRange[cellRange.length-1].getRow(); int firstSelectedColumn = cellRange[0].getColumn(); int lastSelectedColumn = cellRange[cellRange.length-1].getColumn(); if(job != null) { // start the print job if (!printer.startPrint()) return; // tell the printer which row and column to start printing int currentRow = firstSelectedRow; int currentColumn = firstSelectedColumn; int x1 = -1; // loop through the selection while(currentRow <= lastSelectedRow) { while(currentColumn <= lastSelectedColumn) { record = new DataSubsetRecord(); record.firstRow = currentRow; record.firstColumn = currentColumn; record.x1 = x1; // calculate the number of rows and columns that will fit if (!printer.calcPageBounds(record)) // make sure that you do not print more than what has been selected if (record.lastColumn > lastSelectedColumn) { record.lastColumn = lastSelectedColumn; record.needsCalc = true; } if (record.LastRow > lastSelectedRow) { record.LastRow = lastSelectedRow; record.needsRecalc = true; } g.translate(36,36); // print the page and send it to the printer // if we did not change record.lastRow, record.lastColumn // or record.needsRecalc, then we do not need to pass record, // but because we might have changed it, we will pass it here printer.printPage(g, record); g.dispose(); // if the last column does not fit on the page, // then set up to start printing where we left off if(record.x2 >= 0){ x1 = record.x2; } else { //the last column is complete //so set up to start printing next column currentColumn = record.lastColumn + 1; x1 = -1; } } // end of column loop // set up to start printing at the beginning of the next row currentRow = record.lastRow + 1; currentColumn = 0; } // end of row loop //end the job job.end(); printer.endPrint(); //call endPrint at the end } //end if printjob