Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing of Nested Table is possible?  RSS feed

 
Mohammed Ajmal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a Nested table which is having tables with in tables.

When I try to print these table. It doesn;t print the nested table.
So, columns till the other table comes are printed, rest is ignored.

Can anyone throw light on these. Is nested table printing possible. If so how to take care of such conditions.

Any articles or links are greatly appreciated.

Thanks,
Ajmal
 
Kriti Garg
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Kindly elaborate or send ur code so that it can be debugged.

Kriti
 
Mohammed Ajmal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The component is getting displayed correctly but when I am printing only few details are printed.

Below is snapshot of code : It is little difficult to understand.. please do read...


// Class which displays the EmpDetails Dialog which has employee details...
// In the Dailog box print and preview buttons are there.




class EmpDetails implements Printable extends JDialog
{

public EmpDetails ( Controller c , Vector EmpDetailsList)
{

jPanelObj.setLayout( new GridBagLayout() );
jPanelObj.setBorder(BorderFactory.createEtchedBorder());

// Table model... is a class which extends NestedTable and
// it works fine while displaying the table in frame..

NestedTableModel = Factory.getNestedTableModel(tableName);
NestedTableModel.setData(empDetailsList);

// NestedTable is an user defined object which extends JTable
// and insert the nested table if given its table model
// it defines method to setColumn widths , rows etc...
// It display the table into pane correctly which in non editable..
//


try
{
NestedTable = Factory.getNestedTable(tableName);
NestedTable.setAutoResizeMode ( JTable.AUTO_RESIZE_OFF );
NestedTable.setColumnWidths();
NestedTable.sizeColumnsToFit();
NestedTable.setRowHeights();
}
catch (NestedTableModelNotFoundException ne)
{
ne.printStackTrace();
// System.exit(0);
}

empDetailsScroll = new JScrollPane(NestedTable); empDetailsScroll.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VER TICAL_SCROLLBAR_ALWAYS);
empDetailsScroll.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.H ORIZONTAL_SCROLLBAR_ALWAYS);

NestedTable.setPreferredScrollableViewportSize(new Dimension(NestedTable.getTableWidth(), 400));


empDetailsScroll.setPreferredSize(dim);


getContentPane().add( JPanel1, contentPaneConst );

// Add scrollpane to panel.
jPanelObj.add( empDetailsScroll, detailsConst );

getContentPane().setLayout( new GridBagLayout() );

// Add to content pane..

getContentPane().add( jPanelobj, contentPaneConst );

}


Note: The table has columns which are displayed in the pane correctly and it has print and printpreview buttons respectively.....

if I select a list of empDetails and click DetailsView it displays them correctly in the pane.
In the pane If I click the print button it displays only first empDetails is displayed with remaining not visible....

Here the empDetails Object is implementing "Printable" interface. So passing the complete object to it..

Below is the print method which I am using.

public void print_action(java.awt.event.ActionEvent event)
{

PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(EmpDetails.this);
pf = pj.validatePage(pf);
pj.setJobName("Plan Details Table");
pj.printDialog();

try
{
pj.print();
}

................
..............

}


public int print(Graphics g, PageFormat pageFormat,
int pageIndex)
throws PrinterException
{


System.err.println("begin time to print is " + System.currentTimeMillis());

Jtable tableView = new JTable();
// Table which is formed earlier which has nested is displayed is again used
// otherwise it is flushing Details window screen after printing...

tableView = printNestedTable;

JScrollPane scrollpane = new JScrollPane(tableView);
frame.getContentPane().setLayout(
new BorderLayout());
frame.getContentPane().add(
BorderLayout.CENTER,scrollpane);
frame.pack();

Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);

Line2D.Double line = new Line2D.Double();

boolean lastPage = false;
int lastRowPrinted;
int numRowsLeft = 0;

//<do calculations>
if(firstPage)
{
fontHeight = g2.getFontMetrics().getHeight();
fontDesent = g2.getFontMetrics().getDescent();

//leave room for page number

pageHeight = pageFormat.getImageableHeight() - bottomMargin;

pageWidth = pageFormat.getImageableWidth();
tableWidth = (double) tableView.getColumnModel().getTotalColumnWidth();
scale = 1;

if (tableWidth >= pageWidth)

{
scale = pageWidth / tableWidth;
}

headerHeightOnPage = tableView.getTableHeader().getHeight()*scale;
tableWidthOnPage = tableWidth*scale;
oneRowHeight = ( tableView.getRowHeight())*scale;
numRowsOnAPage = (int)( ( pageHeight - headerHeightOnPage )/oneRowHeight );
System.err.println("oneRowHeight" + oneRowHeight);
pageHeightForTable = oneRowHeight*numRowsOnAPage;
totalNumPages = (int)Math.ceil( ( (double)tableView.getRowCount() )/numRowsOnAPage );
firstPage = false;
}

//</do calculations>

if(pageIndex >= totalNumPages)
{
return Printable.NO_SUCH_PAGE;
}

if(pageIndex + 1 == totalNumPages)
{
lastPage = true;
lastRowPrinted = numRowsOnAPage * pageIndex;
numRowsLeft = tableView.getRowCount() - lastRowPrinted;

}

//<draw the components>

g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2.drawString("Page: "+(pageIndex+1),(int)pageWidth/2-35,
(int)(pageFormat.getImageableHeight() - fontDesent));//bottom center


if(lastPage)
{
line.setLine(0, 0, 0, numRowsLeft * oneRowHeight + headerHeightOnPage );

g2.draw(line);

}
else
{
line.setLine(0, 0, 0, pageHeightForTable + headerHeightOnPage );

g2.draw(line);
}


line.setLine(0,0,tableWidthOnPage,0);
g2.draw(line);

g2.translate(0f,headerHeightOnPage + 0.5);

g2.translate(0.5f,-pageIndex*pageHeightForTable);


//If this piece of the table is smaller than the size available,
//clip to the appropriate bounds.
if (lastPage)
{

g2.setClip(0, (int)(pageHeightForTable * pageIndex),
(int) Math.ceil(tableWidthOnPage),
(int) Math.ceil(oneRowHeight * numRowsLeft));


}
//else clip to the entire area available.
else
{
g2.setClip( 0, (int)(pageHeightForTable*pageIndex),
(int) Math.ceil(tableWidthOnPage), (int) Math.ceil(pageHeightForTable));
}

g2.scale(scale,scale);

RepaintManager.currentManager(frame).setDoubleBufferingEnabled(false);
tableView.paint(g2);

g2.scale(1/scale,1/scale);
g2.translate(0f,pageIndex*pageHeightForTable);
g2.translate(0f, -headerHeightOnPage);

g2.setClip(0, 0,(int) Math.ceil(tableWidthOnPage),
(int)Math.ceil(headerHeightOnPage));
g2.scale(scale,scale);


tableView.getTableHeader().paint(g2);//paint header at top

//</draw the components


System.gc();
System.err.println("end time taken to print the screen is " + System.currentTimeMillis());


return Printable.PAGE_EXISTS;
}

} //


snapshot of Table in dialog component:

------------------------------------------------------------------------------- ----------------------
EmpNo | Personal Details Table | EmpLocation | EmpDepart | ....

------------------------------------------------------------------------------- ----------------------
20 |Name | DOB | Qualification | ... | India | IT | .......
-----------------------------------
ABC | 20/01/78 | BS | ...
- ---


The details are visible in the dialog , but when I print it displays Header and first row and rest is not printed.. Morever I am using the same methods for simple tables they are printing it, but I am not getting any idea where is the problem and why is this occurring.

Also, is the print() method above can be modified or any better print method is there so performance canbe improved and also to print correctly for the nested table.

I tried many alternatives and couldnot succeed. Please help me in these regard.

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!