如何打印一个JTable对象的Java应用程序 [英] How to print a JTable object in the Java application

查看:384
本文介绍了如何打印一个JTable对象的Java应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,一旦数据从数据库中读取JTable中的对象表嵌入在滚动,我们如何创建一个打印作业,使得它可以显示出打印显示的表因此在A3尺寸的纸张?

我的code从数据库中提取数据如下:

 尝试
{
    的Class.forName(com.mysql.jdbc.Driver);
    连接CON =的DriverManager.getConnection(JDBC的:mysql://本地主机/福利局,根,传);
    声明STAT = con.createStatement();
    ResultSet的解析度= stat.executeQuery(SELECT * FROM表,其中名称='+姓名+');
    ResultSetMetaData的RSMD = res.getMetaData();
    INT colcount = rsmd.getColumnCount();
    矢量列=新的向量(colcount);
        的for(int i = 3; I< = colcount;我++)
    {
        columns.add(rsmd.getColumnName(I));
    }
    矢量数据=新的向量();
    向量列;    //存储行数据
    而(res.next())
    {
        行=新的向量(colcount);
        的for(int i = 3; I< = colcount;我++)
        {
            row.add(res.getString(I));
        }
        data.add(行);
    }
    表=新的JTable(数据列);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    scrollPane.setViewportView(表);
}
赶上(异常前)
{
    的System.out.println(除息);
}

我使用Vector类来从表中的数据。我们如何打印在显示表中显示一个文件中的数据?


解决方案

您显然不阅读提供的链接你的<一个href=\"http://stackoverflow.com/questions/14543695/how-to-make-a-scrollable-jtable/14543806#14543806\">$p$pvious问题。

如何使用表打印部分


  

打印


  
  

JTable中提供的打印表格的简单API。最简单的方法
  打印出的表格是调用JTable.print不带参数:


  {尝试
     如果(!table.print()){
         通信System.err.println(用户已取消打印);
     }
 }赶上(java.awt.print.PrinterException E){
     System.err.format(无法​​打印%S%N,e.getMessage());
 }


  

调用打印在一个普通的Swing应用程序带来了一个标准打印
  对话框。 (在无头应用,该表被简单地打印。)
  返回值指示用户是否与打印径自
  作业或取消它。 JTable.print可以抛出
  java.awt.print.PrinterException,这是一个检查的异常;那是
  为什么上面的示例使用的try ... catch。


  
  

JTable中提供了各种选项打印的几个重载。该
  从TablePrintDemo.java以下code显示了如何定义一个页面
  标题:


  
  

MessageFormat头=新MessageFormat(第{0,数字,整数});


  {尝试
    table.print(JTable.PrintMode.FIT_WIDTH,头,NULL);
}赶上(java.awt.print.PrinterException E){
     System.err.format(无法​​打印%S%N,e.getMessage());
}


  

对于更复杂的打印应用,使用JTable.getPrintable获得
  可打印对象的表。更多关于打印,参考
  印刷课在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.

这篇关于如何打印一个JTable对象的Java应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆