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

[4239 byte] By [jmchris1a] at [2007-11-27 7:27:39]
# 1

A component can only have one parent. In your print method you add the table to a new JFrame, which means it gets removed from your other frame.

In your print frame, instead of adding the existing table to the frame, create a new table by using:

JTable printTable = new JTable(detail.getModel());

camickra at 2007-7-12 19:07:55 > top of Java-index,Java Essentials,Java Programming...
# 2
Great! That worked like a charm. I truly appreciate your input. I spent hours trying to find a way to make that work, so I can't thank you enough.
jmchris1a at 2007-7-12 19:07:55 > top of Java-index,Java Essentials,Java Programming...