如何在 Java 应用程序中打印 JTable 对象 [英] How to print a JTable object in the Java application
问题描述
问题 现在,一旦从数据库中获取数据并显示在嵌入在滚动窗格中的 JTable 对象表"中,我们如何创建打印作业以打印显示的表在 A3 大小的纸上?
我从数据库中获取数据的代码如下所示:
试试{Class.forName("com.mysql.jdbc.Driver");Connection con=DriverManager.getConnection("jdbc:mysql://localhost/newb","root","pass");语句 stat=con.createStatement();ResultSet res=stat.executeQuery("select * from table where name = '"+name+"'");结果集元数据 rsmd = res.getMetaData();int colcount = rsmd.getColumnCount();向量列 = 新向量(列数);for(int i=3; i<=colcount; i++){column.add(rsmd.getColumnName(i));}矢量数据 = new Vector();向量行;//存储行数据而(res.next()){行 = 新向量(列数);for(int i=3; i<=colcount; i++){row.add(res.getString(i));}数据添加(行);}table = new JTable(data, columns);table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);scrollPane.setViewportView(table);}捕获(异常前){System.out.println(ex);}
我正在使用向量类从表中获取数据.我们如何将显示表格中显示的数据打印到纸上?
您显然没有阅读 上一个问题.
<块引用>打印
JTable 提供了一个简单的 API 来打印表格.最简单的方法打印出一个表是调用 JTable.print 不带参数:
尝试{如果(!table.print()){System.err.println("用户取消打印");}} catch (java.awt.print.PrinterException e) {System.err.format("无法打印 %s%n", e.getMessage());}
<块引用>
在普通 Swing 应用程序上调用打印会产生标准打印对话框.(在无头应用程序中,表格只是打印出来的.)返回值指示用户是否继续打印工作或取消它.JTable.print 可以抛出java.awt.print.PrinterException,这是一个检查异常;那是为什么上面的例子使用了 try ... catch.
JTable 提供了多种带有各种选项的打印重载.这TablePrintDemo.java 中的以下代码显示了如何定义页面标题:
MessageFormat header = new MessageFormat("Page {0,number,integer}");
尝试{table.print(JTable.PrintMode.FIT_WIDTH, header, null);} catch (java.awt.print.PrinterException e) {System.err.format("无法打印 %s%n", e.getMessage());}
<块引用>
对于更复杂的打印应用,使用 JTable.getPrintable 来获取表格的 Printable 对象.有关可打印的更多信息,请参阅2D 图形路径中的打印课程.
Question Now once the data is fetched from the database and shown in the JTable object "table" embedded in the scrollPane, how do we create a print job that makes it possible to print the displayed table as such in A3 sized paper ?
My code to fetch the data from the database is shown below:
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/newb","root","pass");
Statement stat=con.createStatement();
ResultSet res=stat.executeQuery("select * from table where name = '"+name+"'");
ResultSetMetaData rsmd = res.getMetaData();
int colcount = rsmd.getColumnCount();
Vector columns = new Vector(colcount);
for(int i=3; i<=colcount; i++)
{
columns.add(rsmd.getColumnName(i));
}
Vector data = new Vector();
Vector row;
// Store row data
while(res.next())
{
row = new Vector(colcount);
for(int i=3; i<=colcount; i++)
{
row.add(res.getString(i));
}
data.add(row);
}
table = new JTable(data, columns);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane.setViewportView(table);
}
catch(Exception ex)
{
System.out.println(ex);
}
I am using vector class to fetch the data from the table. How do we print the data shown in the displayed table to a paper?
You obviously didn't read the links provided in your previous question.
From the Printing section of How to use Tables
Printing
JTable provides a simple API for printing tables. The easiest way to print out a table is to invoke JTable.print with no arguments:
try {
if (! table.print()) {
System.err.println("User cancelled printing");
}
} catch (java.awt.print.PrinterException e) {
System.err.format("Cannot print %s%n", e.getMessage());
}
Invoking print on a normal Swing application brings up a standard printing dialog box. (On a headless application, the table is simply printed.) The return value indicates whether the user went ahead with the print job or cancelled it. JTable.print can throw java.awt.print.PrinterException, which is a checked exception; that's why the above example uses a try ... catch.
JTable provides several overloads of print with various options. The following code from TablePrintDemo.java shows how to define a page header:
MessageFormat header = new MessageFormat("Page {0,number,integer}");
try {
table.print(JTable.PrintMode.FIT_WIDTH, header, null);
} catch (java.awt.print.PrinterException e) {
System.err.format("Cannot print %s%n", e.getMessage());
}
For more sophisticated printing applications, use JTable.getPrintable to obtain a Printable object for the table. For more on Printable, refer to the Printing lesson in the 2D Graphics trail.
这篇关于如何在 Java 应用程序中打印 JTable 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!