Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java应用程序中打印JTable对象_Java_Swing_Printing_Jtable_Awt - Fatal编程技术网

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

如何在Java应用程序中打印JTable对象,java,swing,printing,jtable,awt,Java,Swing,Printing,Jtable,Awt,问题现在,一旦数据从数据库中取出并显示在滚动窗格中嵌入的JTable对象“table”中,我们如何创建打印作业,以便能够以A3大小的纸张打印显示的表格 我从数据库获取数据的代码如下所示: try { Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost/newb","root","pass"); Stateme

问题现在,一旦数据从数据库中取出并显示在滚动窗格中嵌入的JTable对象“table”中,我们如何创建打印作业,以便能够以A3大小的纸张打印显示的表格

我从数据库获取数据的代码如下所示:

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);
}
试试看
{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost/newb“,”根“,”通过“);
Statement stat=con.createStatement();
ResultSet res=stat.executeQuery(“从表中选择*,其中name='”+name+'”);
ResultSetMetaData rsmd=res.getMetaData();
int colcount=rsmd.getColumnCount();
向量列=新向量(colcount);

对于(int i=3;i只需使用
JTable.print()
方法即可。这里有一个和您显然没有阅读您的

来自

印刷品

JTable为打印表提供了一个简单的API 打印出要调用JTable.print而不带参数的表:

在普通Swing应用程序上调用print会带来标准打印 对话框。(在无头应用程序上,只需打印表格。) 返回值指示用户是否继续打印 作业或取消作业。JTable.print可以抛出 java.awt.print.PrinterException,这是一个选中的异常;这是 为什么上面的示例使用try…catch

JTable提供了多个带有各种选项的打印重载 以下来自TablePrintDemo.java的代码显示了如何定义页面 标题:

MessageFormat头=新的MessageFormat(“第{0页,数字,整数}”)

对于更复杂的打印应用程序,请使用JTable.getPrintable获得 表的可打印对象。有关可打印的详细信息,请参阅 在2D图形轨迹中打印课程


我希望这段代码能帮助您尝试如何在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);
    }
}                                         

另请参见。但仅使用此功能是否有效?“未找到打印服务”。这是我调用此功能时显示的内容。
try {
    table.print(JTable.PrintMode.FIT_WIDTH, header, null); 
} catch (java.awt.print.PrinterException e) {
     System.err.format("Cannot print %s%n", e.getMessage()); 
}
    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);
    }
}