Loosing my JTable after I print
I am working on an application in which I query a database, then display the information in a JTable. If the user desires, they can hit the PRINT button which will print the table for them.
The problem I am having is that after the user hits OK on the print dialog box, the JTable they are working with disappears! It print fine, but for some reason I cannot go back in and re-create or edit the table without destroying the whole object and starting over. Any suggestions?
/*
* PanelPrinter.java
*
* Created on June 12, 2007, 10:03 AM
*
*
*/
package Catalyst;
import java.awt.print.*;
import java.awt.*;
import javax.swing.*;
import java.util.Date;
/**
*
* @author JMChris1
*/
publicclass PanelPrinterimplements Printable{
/** Creates a new instance of PanelPrinter */
public PanelPrinter( JPanel description, JTable detail ){
// Initialize local objects
_descriptionPanel = description;
_detailTable = detail;
}// End of ctor
publicint print( Graphics g, PageFormat pf,int page )
throws PrinterException{
// If there is a page to print, then print it
if (page > 0){
// We have only one page, and 'page' is zero-based
return( NO_SUCH_PAGE );
}// End of if statement
// Force the page into landscape mode
pf.setOrientation( PageFormat.LANDSCAPE );
// User (0,0) is typically outside the imageable area, so we must
//translate by the X and Y values in the PageFormat to avoid clipping
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
// Create the username, and date/time stamp for the bottom of
// the page
Date time =new Date();
JTextField footer =new JTextField( System.getProperty("user.name" ) );
// Print the current display
JFrame frame =new JFrame();
Container container =new Container();
container.setLayout(new BorderLayout( 0, 3 ) );
container = frame.getContentPane();
container.add( _descriptionPanel, BorderLayout.NORTH );
container.add( _detailTable, BorderLayout.CENTER );
container.add( footer, BorderLayout.SOUTH );
frame.pack();
container.printAll( g );
// Re-draw the components on the GUI
_descriptionPanel.revalidate();
_detailTable.revalidate();
// Tell the caller that this page is part of the printed document
return( PAGE_EXISTS );
}// End of print() method
// Declare local objects
JPanel _descriptionPanel;
JTable _detailTable;
}// End of PanelPrinter class

