cannot print JTable column header

Discussion in 'Java' started by cluenek, Jul 18, 2003.

  1. cluenek

    cluenek Guest

    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;
    }
    }
     
    cluenek, Jul 18, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tivo Escobar
    Replies:
    1
    Views:
    7,417
    manusa
    Apr 12, 2007
  2. James Gralton

    JTable resizing row/column cell header

    James Gralton, Jul 16, 2004, in forum: Java
    Replies:
    1
    Views:
    770
    Andrew Thompson
    Jul 16, 2004
  3. Juha Rossi
    Replies:
    0
    Views:
    3,157
    Juha Rossi
    Aug 2, 2004
  4. hansiman

    Image in header column (not replacing column header text)

    hansiman, Feb 5, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    318
    hansiman
    Feb 7, 2004
  5. Replies:
    13
    Views:
    1,494
    Stuart
    Sep 23, 2012
Loading...

Share This Page