3

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Roshan George
  • 187
  • 2
  • 2
  • 13

3 Answers3

3

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.

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

just use JTable.print() method. here is an article about sending JTable into printer and another one with more parameters

mantrid
  • 2,830
  • 21
  • 18
0

i hope help you with this code try it its for How to print JTable in Java netbeans

    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);
    }
}                                         
Said Erraoudy
  • 1,490
  • 1
  • 13
  • 21