Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > cannot print JTable column header

Reply
Thread Tools

cannot print JTable column header

 
 
cluenek
Guest
Posts: n/a
 
      07-18-2003
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().getTotalColumnW idth();
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;
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CheckBox in Column of JTable: Exception: java.lang.String cannot becast to java.lang.Boolean clusardi2k@aol.com Java 13 09-23-2012 02:44 PM
JTable not refreshing column header cgokey@gcmd.nasa.gov Java 1 03-06-2005 03:01 PM
Print JTable - last row gets splitted & header-String problem Juha Rossi Java 0 08-02-2004 08:15 AM
JTable resizing row/column cell header James Gralton Java 1 07-16-2004 03:53 PM
Image in header column (not replacing column header text) hansiman ASP .Net Datagrid Control 3 02-07-2004 12:17 AM



Advertisments