cannot print JTable column header

C

cluenek

Hello Folks,

I'm Fairly new to java. I"m using JBuilder 3, java 1.2. My goal is
to print out a GridControl. I referenced Sun's tutorial on how to
print a JTable. Not to much info. on printing gridcontrols out there.
I'm basically instantiating a PrintData class with a Gridcontol.
Then setting the JdbTable dataset to pick up the rows, columns and
headers. All prints fine except for the header. The header does seem
to be set. When testing I can get columns names. Any insight would
be much appreciated.

Thanks

public class PrintData implements Printable
{
private GridControl gridControlToPrint;
private final static int POINTS_PER_INCH = 27;

public PrintData(GridControl aGridControl)
{
gridControlToPrint = aGridControl;
}

private JTable modifiedTable()
{
JdbTable tempTable = new JdbTable();

tempTable.setDataSet(gridControlToPrint.getDataSet());
tempTable.setSize(gridControlToPrint.getSize());

for (int row = 0; row < tempTable.getRowCount(); row++)
{
for (int col = 0; col < tempTable.getColumnCount(); col++)
{
Object temp = tempTable.getValueAt(row,col);

if (temp == null)
tempTable.setValueAt(" ",row,col);
else
tempTable.setValueAt(temp.toString(),row,col);
}
}

return (JTable)tempTable;
}

public int print(Graphics g, PageFormat pageFormat, int page)
{
return printTable(g, pageFormat, page);
}

private int printTable(Graphics g, PageFormat pageFormat, int
pageIndex)
{
JTable tableView = modifiedTable();

Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
int fontHeight = g2.getFontMetrics().getHeight();
int fontDesent = g2.getFontMetrics().getDescent();
double pageHeight = pageFormat.getImageableHeight() -
fontHeight;
double pageWidth = pageFormat.getImageableWidth();
double tableWidth =
(double)tableView.getColumnModel().getTotalColumnWidth();
double scale = 1;

if (tableWidth >= pageWidth)
scale = pageWidth / tableWidth;

double headerHeightOnPage =
tableView.getTableHeader().getHeight() * scale;
double tableWidthOnPage = tableWidth * scale;
double oneRowHeight =(tableView.getRowHeight() +
tableView.getRowMargin()) * scale;
int numRowsOnAPage = (int) ((pageHeight - headerHeightOnPage)
/ oneRowHeight);
double pageHeightForTable = oneRowHeight * numRowsOnAPage;
int totalNumPages = (int) Math.ceil(((double)
tableView.getRowCount()) / numRowsOnAPage);

if (pageIndex >= totalNumPages)
return NO_SUCH_PAGE;

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

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

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

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

g2.scale(scale, scale);
tableView.print(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().print(g2);

return Printable.PAGE_EXISTS;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top