利用Java Api打印JTable
参考书籍完成的一个可以打印一个单独JTable组件的涵数,希望对大家有所帮助.
private void printTable() {
Toolkit kit = Toolkit.getDefaultToolkit(); //获取工具箱
Properties props = new Properties();
props.put("awt.print.printer", "durango"); //设置打印属性
props.put("awt.print.numCopies", "2");
if (kit != null) {
//获取工具箱自带的打印对象
PrintJob printJob = kit.getPrintJob(this, "打印 页面", props);
if (printJob != null) {
Graphics pg = printJob.getGraphics(); //获取打印对象的图形环境
Graphics2D g2 = (Graphics2D) pg; ///
PageFormat pf = new PageFormat(); ///
g2.translate(pf.getImageableX(), pf.getImageableY()); ///转换坐标,确定打印边界
if (pg != null) {
try {
pg.dispose(); // Shoot the page to printer
this.jScrollPane14.printAll(pg); //打印该窗体的组件
}
finally {
pg.dispose(); //注销图形环境pageIndex
}
}
printJob.end(); //结束打印作业
}
}
}