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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14544013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:36:45  来源:igfitidea点击:

How to print a JTable object in the Java application

javaswingprintingjtableawt

提问by Roshan George

QuestionNow 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?

我正在使用向量类从表中获取数据。我们如何将显示表格中显示的数据打印到纸上?

回答by mantrid

回答by MadProgrammer

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:

印刷

JTable 提供了一个简单的 API 来打印表格。打印表的最简单方法是不带参数调用 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. (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}");

在普通 Swing 应用程序上调用打印会显示一个标准的打印对话框。(在无头应用程序上,该表只是打印。)返回值指示用户是继续打印作业还是取消了它。JTable.print 可以抛出 java.awt.print.PrinterException,这是一个已检查的异常;这就是为什么上面的例子使用 try ... catch 的原因。

JTable 提供了多种带有各种选项的打印重载。TablePrintDemo.java 中的以下代码显示了如何定义页眉:

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.

对于更复杂的打印应用程序,使用 JTable.getPrintable 来获取表格的 Printable 对象。有关可打印的更多信息,请参阅 2D 图形路径中的打印课程。

回答by Said Erraoudy

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);
    }
}