![]() | ![]() | ![]() | ![]() | ![]() |
The TableView displays data from different types of tabular data sources and enables users to interact with live data. In this sample, the TableView will be extended to support printing to PDF file.
The Java class in this sample creates and displays a simple table view. For simplicity, this sample uses its own static data model for the TableView. In most real-world use cases, users will connect to live data and not create their own data model. For more information about how to connect the TableView to live data, see Sample 25994.
Here are the steps for creating a simple TableView:
The resulting code would look something like this:
...
Create arrays with row data and column names
...
DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
TableView tableView1 = new TableView(model);
|
The code shown on the Full Code tab will create a DefaultTableModel and display a TableView with that model. The TableView will be extended to support printing to PDF file.
This TableView is extended to support printing the contents to a PDF file. In order to support printing the TableView to PDF, the TableView has to be subclassed because it needs to implement the com.sas.awt.print.PrintableInterface. The com.sas.awt.print.PDFGraphics is used to render the table in the PDF file format.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Tip: For help with building a SAS Java Project, see SAS Note 32218.
private static String ACROBAT_LOCATION = "C:\\Program Files\\Adobe\\Reader 8.0\\Reader\\AcroRd32.exe";
package samples;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.TableColumnModel;
import com.sas.swing.visuals.tableview.ColumnHeader;
import com.sas.swing.visuals.tableview.RowHeader;
public class PrintToPDFTableViewApp {
private static String ACROBAT_LOCATION = "C:\\Program Files\\Adobe\\Reader 8.0\\Reader\\AcroRd32.exe";
protected PDFPrintingTable PDFPrintingTable1;
protected JScrollPane JScrollPane1;
public static void main(String[] args) {
// set swing look and feel with system look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
PrintToPDFTableViewApp simpleApp = new PrintToPDFTableViewApp();
simpleApp.createFrame(args).setVisible(true);
}
// create a frame and add the table view to it
public JFrame createFrame(String[] args) {
JFrame frame = new JFrame(this.getClass().getName());
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
// for the simplicity of this sample, create some static data
// in real-world use cases, the user would most likely connect to live
// data
String data1[] = { "Bob", "42", "M", "Sailing" };
String data2[] = { "Mary", "33", "F", "Gardening" };
String data3[] = { "Jane", "27", "F", "Shopping" };
String data4[] = { "Tom", "65", "M", "Reading" };
String data5[] = { "Dave", "23", "M", "Skiing" };
String rowData[][] = { data1, data2, data3, data4, data5 };
String columnNames[] = { "Name", "Age", "Sex", "Hobby" };
// create a new TableView that will print to PDF
// PDFPrintingTable is a subclass of TableView
// we define PDFPrintingTable below
PDFPrintingTable1 = new PDFPrintingTable(rowData, columnNames);
// create a new scroll pane with the table view
JScrollPane scrollPane = new JScrollPane(PDFPrintingTable1);
contentPane.add(scrollPane, BorderLayout.CENTER);
contentPane.setSize(500, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
// print the table view to PDF
// this happens automatically when the program comes up
// but you could add a button to perform the printing
printToPDF();
return frame;
}
public void printToPDF() {
// set the correct fonts for AcrobatReader
java.awt.Font font = new Font("SansSerif", java.awt.Font.PLAIN, 11);
PDFPrintingTable1.setFont(font);
PDFPrintingTable1.getColumnHeader().setFont(font);
PDFPrintingTable1.getRowHeader().setFont(font);
PDFPrintingTable1.getColumnHeader().sizeColumnsToFit();
PDFPrintingTable1.getRowHeader().sizeRowsToFit(true, true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Get a PrintJob object
com.sas.awt.print.PDFGraphics pdfGraphics = new com.sas.awt.print.PDFGraphics(
ACROBAT_LOCATION);
com.sas.awt.print.PrintJob pj = com.sas.awt.print.PrintJob
.getPrintJob(pdfGraphics);
pj.setPrintable(PDFPrintingTable1);
try {
pj.print();
} catch (com.sas.awt.print.PrintException pe) {
pe.printStackTrace();
}
}
});
}
// subclass of TableView that allows printing the contents to PDF
public class PDFPrintingTable extends
com.sas.swing.visuals.tableview.TableView implements
com.sas.awt.print.PrintableInterface {
PDFPrintingTable(Object[][] rowData, Object[] columnNames) {
super(rowData, columnNames);
}
private Graphics graph;
// Defined in com.sas.awt.print.PrintableInterface
public java.awt.Panel getPrintOptionsPanel() {
// overwrite the following line with your own implementation
return null;
}
// Defined in com.sas.awt.print.PrintableInterface
public java.awt.Rectangle getPageBounds(java.awt.Graphics p1, int p2,
int p3, int p4) {
// overwrite the following line with your own implementation
return null;
}
// Defined in com.sas.awt.print.PrintableInterface
public void printInitialize(java.awt.Graphics p1)
throws com.sas.awt.print.PrintException {
graph = p1;
}
// Defined in com.sas.awt.print.PrintableInterface
public boolean pageExists(int p1) {
if (p1 == 1 && (getRowCount() > 0 && getColumnCount() > 0))
return true;
return printInfo.m_pages > (p1 - 1);
}
// Defined in com.sas.awt.print.PrintableInterface
public void print(Graphics g, int pageIndex, int pageWidth,
int pageHeight) throws com.sas.awt.print.PrintException {
if (printInfo == null || !printInfo.pageDone) {
getPageInfo(g, pageWidth, pageHeight);
}
g.setFont(printInfo.printFont);
if (pageIndex - 1 >= printInfo.m_pages) {
printInfo.pageDone = false;
return;
}
if (printInfo.prevPageIndex != pageIndex - 1) {
printInfo.subPageIndex++;
if (printInfo.subPageIndex == printInfo.colSplits) {
printInfo.subPageIndex = 0;
}
}
int rowIndex = (pageIndex - 1) / (printInfo.colSplits);
printSubTable(g, rowIndex, printInfo.subPageIndex);
printInfo.prevPageIndex = pageIndex - 1;
return;
}
// Defined in com.sas.awt.print.PrintableInterface
public void printFinalize() {
printInfo = null;
}
class PrintInfo {
public boolean pageDone;
public Font printFont;
public List colLayout;
public List rowLayout;
public int colSplits;
public int rowSplits;
public int subPageIndex;
public int prevPageIndex;
public int m_pages;
public int pageHeight;
public int pageWidth;
public int columnHeaderHeight;
public int rowHeaderWidth;
public int heldRowTopHeight;
public int heldRowBottomHeight;
public int heldColumnLeftWidth;
public int heldColumnRightWidth;
public int heldColumnLeftCount;
public int heldColumnRightCount;
public int heldRowTopCount;
public int heldRowBottomCount;
public int heldRowHeight;
public int heldColumnWidth;
}
private PrintInfo printInfo;
private void getPageInfo(Graphics g, int pageWidth, int pageHeight) {
if (printInfo == null)
printInfo = new PrintInfo();
printInfo.subPageIndex = 0;
printInfo.colLayout = null;
printInfo.colSplits = 0;
printInfo.subPageIndex = 0;
printInfo.prevPageIndex = 0;
printInfo.printFont = getFont();
g.setFont(printInfo.printFont);
TableColumnModel tableColumnModel = tableHeader.getColumnModel();
int colCount = tableColumnModel.getColumnCount();
int columnMargin = tableColumnModel.getColumnMargin();
printInfo.heldRowTopHeight = getRowHeader().getTopHeldHeight();
printInfo.heldRowBottomHeight = getRowHeader()
.getBottomHeldHeight();
printInfo.heldColumnLeftWidth = getColumnHeader()
.getLeadingHeldWidth();
printInfo.heldColumnRightWidth = getColumnHeader()
.getTrailingHeldWidth();
printInfo.heldColumnLeftCount = 0;
printInfo.heldColumnRightCount = 0;
if (getColumnHeader() != null) {
printInfo.heldColumnLeftCount = getColumnHeader()
.getHeldIndices(SwingConstants.LEADING).size();
printInfo.heldColumnRightCount = getColumnHeader()
.getHeldIndices(SwingConstants.TRAILING).size();
}
printInfo.heldRowTopCount = getRowHeader().getHeldIndices(
SwingConstants.TOP).size();
printInfo.heldRowBottomCount = getRowHeader().getHeldIndices(
SwingConstants.BOTTOM).size();
printInfo.heldRowHeight = printInfo.heldRowTopHeight
+ printInfo.heldRowBottomHeight;
printInfo.heldColumnWidth = printInfo.heldColumnLeftWidth
+ printInfo.heldColumnRightWidth;
printInfo.columnHeaderHeight = getTableHeader().getHeight();
printInfo.rowHeaderWidth = getRowHeader().getWidth();
printInfo.pageHeight = pageHeight;
printInfo.pageWidth = pageWidth;
int hgt = printInfo.columnHeaderHeight + printInfo.heldRowHeight;
int yPos = 0; // position relative to top of table
printInfo.rowLayout = new ArrayList();
printInfo.rowLayout.add(new Integer(0));
printInfo.rowSplits = 0;
int rowHgt;
for (int row = printInfo.heldRowTopCount, rowCount = getRowCount(); row < rowCount
- printInfo.heldRowBottomCount; row++) {
rowHgt = getRowHeight(row);
if (hgt + rowHgt > printInfo.pageHeight && yPos != 0) {
printInfo.rowSplits++;
printInfo.rowLayout
.add(new Integer(
yPos
+ (printInfo.rowSplits * (printInfo.heldRowHeight))));
hgt = printInfo.columnHeaderHeight
+ printInfo.heldRowHeight;
}
hgt += rowHgt;
yPos += rowHgt;
}
if (hgt > 0) // if there is more columns
{
printInfo.rowLayout
.add(new Integer(
yPos
+ ((printInfo.rowSplits + 1) * printInfo.heldRowHeight)));
printInfo.rowSplits++;
}
int wid = printInfo.rowHeaderWidth + printInfo.heldColumnWidth;
int xPos = 0; // position relative to top of table
printInfo.colLayout = new ArrayList();
printInfo.colLayout.add(new Integer(0));
printInfo.colSplits = 0;
int colWid;
for (int col = printInfo.heldColumnLeftCount; col < colCount
- printInfo.heldColumnRightCount; col++) {
colWid = tableColumnModel.getColumn(col).getWidth();
if (wid + colWid > printInfo.pageWidth && xPos != 0) {
printInfo.colSplits++;
printInfo.colLayout
.add(new Integer(
xPos
+ (printInfo.colSplits * (printInfo.heldColumnWidth))));
wid = printInfo.rowHeaderWidth + printInfo.heldColumnWidth;
}
wid += colWid;
xPos += colWid;
}
if (wid > 0) // if there is more columns
{
printInfo.colLayout
.add(new Integer(
xPos
+ ((printInfo.colSplits + 1) * printInfo.heldColumnWidth)));
printInfo.colSplits++;
}
printInfo.m_pages = printInfo.rowSplits * printInfo.colSplits;
printInfo.pageDone = true;
}
private void printSubTable(Graphics g, int rowIndex, int columnIndex) {
int pageLeft = ((Integer) printInfo.colLayout.get(columnIndex))
.intValue();
int pageRight = ((Integer) printInfo.colLayout.get(columnIndex + 1))
.intValue();
int pageWidth = pageRight - pageLeft;
int pageTop = ((Integer) printInfo.rowLayout.get(rowIndex))
.intValue();
int pageBottom = ((Integer) printInfo.rowLayout.get(rowIndex + 1))
.intValue();
int pageHeight = pageBottom - pageTop;
// print origin
Graphics g2 = g.create(0, 0, printInfo.rowHeaderWidth,
printInfo.columnHeaderHeight);
Component origin = getOriginComponent();
origin.print(g2);
g2.dispose();
// print table header
g2 = g.create(0, 0, pageWidth + printInfo.rowHeaderWidth,
pageHeight + printInfo.columnHeaderHeight);
g2.translate(-pageLeft, 0);
g2.translate(printInfo.rowHeaderWidth, 0);
ColumnHeader header = getColumnHeader();
g2.translate(columnIndex * (printInfo.heldColumnWidth), 0);
g2.setClip(pageLeft - (columnIndex * printInfo.heldColumnWidth), 0,
pageWidth, printInfo.columnHeaderHeight);
header.print(g2);
g2.dispose();
// print row header
g2 = g.create(0, 0, pageWidth + printInfo.rowHeaderWidth,
pageHeight + printInfo.columnHeaderHeight);
g2.translate(0, -pageTop);
g2.translate(0, printInfo.columnHeaderHeight);
RowHeader rowHeader = getRowHeader();
g2.translate(0, rowIndex * (printInfo.heldRowHeight));
g2.setClip(0, pageTop - (rowIndex * printInfo.heldRowHeight),
printInfo.rowHeaderWidth, pageHeight);
rowHeader.print(g2);
g2.dispose();
// print table data
g2 = g.create(0, 0, pageWidth + printInfo.rowHeaderWidth,
pageHeight + printInfo.columnHeaderHeight);
g2.translate(-pageLeft, -pageTop);
g2.translate(printInfo.rowHeaderWidth, 0);
g2.translate(0, printInfo.columnHeaderHeight);
g2.translate(columnIndex * (printInfo.heldColumnWidth), rowIndex
* (printInfo.heldRowHeight));
g2.setClip(pageLeft - (columnIndex * printInfo.heldColumnWidth),
pageTop - (rowIndex * printInfo.heldRowHeight), pageWidth,
pageHeight);
print(g2);
g2.dispose();
}
}
}
|
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
| Type: | Sample |
| Date Modified: | 2008-07-09 16:03:21 |
| Date Created: | 2006-01-02 10:37:14 |
| Product Family | Product | Host | Product Release | SAS Release | ||
| Starting | Ending | Starting | Ending | |||
| SAS System | SAS AppDev Studio | Windows Vista | 3.2 | 9.1 TS1M3 SP4 | ||
| Microsoft Windows XP Professional | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows Server 2003 Standard Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows Server 2003 Enterprise Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows Server 2003 Datacenter Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows NT Workstation | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Professional | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Server | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Datacenter Server | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Advanced Server | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft® Windows® for x64 | 3.2 | 9.1 TS1M3 SP4 | ||||




