简体   繁体   English

如何在Java应用程序中打印JTable对象

[英]How to print a JTable object in the Java application

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 ? 问题现在,一旦从数据库中获取数据并将其显示在scrollPane中嵌入的JTable对象“表”中,我们如何创建一个打印作业,以使其能够在A3尺寸的纸张上打印显示的表格?

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. JTable提供了一个用于打印表的简单API。 The easiest way to print out a table is to invoke JTable.print with no arguments: 打印表的最简单方法是不带任何参数调用JTable.print:

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. 在普通的Swing应用程序上调用打印会弹出一个标准的打印对话框。 (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; JTable.print可以抛出java.awt.print.PrinterException,这是一个已检查的异常; that's why the above example uses a try ... catch. 这就是为什么上面的示例使用try ... catch的原因。

JTable provides several overloads of print with various options. JTable提供了多种带有各种选项的打印重载。 The following code from TablePrintDemo.java shows how to define a page header: TablePrintDemo.java中的以下代码显示了如何定义页面标题:

MessageFormat header = new MessageFormat("Page {0,number,integer}"); MessageFormat标头=新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. 对于更复杂的打印应用程序,请使用JTable.getPrintable获取表的Printable对象。 For more on Printable, refer to the Printing lesson in the 2D Graphics trail. 有关“可打印”的更多信息,请参考“ 2D图形”轨迹中的“打印”课程。

i hope help you with this code try it its for How to print JTable in Java netbeans 我希望此代码可以帮助您尝试一下如何在Java netbeans中打印JTable

    private void btn_printActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
         MessageFormat header = new MessageFormat("Print Report");
        MessageFormat footer = new MessageFormat("Page{0,number,integer}");
    try {
        table_employee.print(JTable.PrintMode.FIT_WIDTH, header, footer);
    } catch (java.awt.print.PrinterAbortException e) {
    } catch (PrinterException ex) {
        Logger.getLogger(employee_info.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                         

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM