(printing jtable) getting jtable to fit on 8X11 piece of paper.

I have read the sun tutorial on printing, and I am able to get jtable to print from applet.  The problem I am having is that the jtable prints on several pieces of paer I am having diffculty in shrinking the jtable to fit on 1 piece of 8X11 paper. I also prints the jatbale in black, causing alot of ink to be used, how can I invert colors so table appers in white and cell data and border black?  I read the the scale method but I am having diffculty in implementaing.  I hav attached my code.

  // for faster printing turn double buffering off

    RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);

    {
      {
        PrinterJob pj = PrinterJob.getPrinterJob();

        pj.setPrintable(rswPlanApplet.this);
        pj.printDialog();

        try {
          pj.print();
        } catch (Exception PrintException) {
        }
      }
    }
    ;

    setVisible(true);
  }

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

    Graphics2D g2 = (Graphics2D) g;

    if (!pageinfoCalculated) {
      getPageInfo(g, pageFormat);
    }

    g2.setColor(Color.red);
    if (pageIndex >= totalNumPages) {
      return NO_SUCH_PAGE;
    }
    if (prevPageIndex != pageIndex) {
      subPageIndex++;
      if (subPageIndex == subTableSplitSize - 1) {
        subPageIndex = 0;
      }
    }

    g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

    int rowIndex = pageIndex / (subTableSplitSize - 1);

    printTablePart(g2, pageFormat, rowIndex, subPageIndex);
    prevPageIndex = pageIndex;

    return Printable.PAGE_EXISTS;
  }

  public void getPageInfo(Graphics g, PageFormat pageFormat) {

    subTableSplit = null;
    subTableSplitSize = 0;
    subPageIndex = 0;
    prevPageIndex = 0;

    fontHeight = g.getFontMetrics().getHeight();
    fontDesent = g.getFontMetrics().getDescent();

    tableHeader = jtBuildPlan.getTableHeader();
    double headerWidth = tableHeader.getWidth();
    headerHeight = tableHeader.getHeight() + jtBuildPlan.getRowMargin();
//      scale(xratio, yratio)
    pageHeight = pageFormat.getImageableHeight();
    pageWidth = pageFormat.getImageableWidth();

    double tableWidth = jtBuildPlan.getColumnModel().getTotalColumnWidth();
    tableHeight = jtBuildPlan.getHeight();
    rowHeight = jtBuildPlan.getRowHeight() + jtBuildPlan.getRowMargin();

    tableHeightOnFullPage = (int) (pageHeight - headerHeight - fontHeight * 2);
    tableHeightOnFullPage = tableHeightOnFullPage / rowHeight * rowHeight;

    TableColumnModel tableColumnModel = tableHeader.getColumnModel();
    int columns = tableColumnModel.getColumnCount();
    int columnMargin = tableColumnModel.getColumnMargin();

    int[] temp = new int[columns];
    int columnIndex = 0;
    temp[0] = 0;
    int columnWidth;
    int length = 0;
    subTableSplitSize = 0;
    while (columnIndex < columns) {

      columnWidth = tableColumnModel.getColumn(columnIndex).getWidth();

      if (length + columnWidth + columnMargin > pageWidth) {
        temp[subTableSplitSize + 1] = temp[subTableSplitSize] + length;
        length = columnWidth;
        subTableSplitSize++;
      } else {
        length += columnWidth + columnMargin;
      }
      columnIndex++;
    } // while

    if (length > 0) { // if are more columns left, part page
      temp[subTableSplitSize + 1] = temp[subTableSplitSize] + length;
      subTableSplitSize++;
    }

    subTableSplitSize++;
    subTableSplit = new int[subTableSplitSize];
    for (int i = 0; i < subTableSplitSize; i++) {
      subTableSplit[i] = temp[i];
    }
    totalNumPages = (int) (tableHeight / tableHeightOnFullPage);
    if (tableHeight % tableHeightOnFullPage >= rowHeight) { // at least 1 more
      // row left
      totalNumPages++;
    }

    totalNumPages *= (subTableSplitSize - 1);
    pageinfoCalculated = true;
  }

  public void printTablePart(Graphics2D g2, PageFormat pageFormat,
      int rowIndex, int columnIndex) {

    String pageNumber = "Page: " + (rowIndex + 1);
    if (subTableSplitSize > 1) {
      pageNumber += "-" + (columnIndex + 1);
    }

    int pageLeft = subTableSplit[columnIndex];
    int pageRight = subTableSplit[columnIndex + 1];

    int pageWidth = pageRight - pageLeft;
    // page number message
    g2.drawString(pageNumber, pageWidth / 2 - 5,
        (int) (pageHeight - fontHeight));

    double clipHeight = Math.min(tableHeightOnFullPage, tableHeight - rowIndex
        * tableHeightOnFullPage);

    g2.translate(-subTableSplit[columnIndex], 0);
    g2.setClip(pageLeft, 0, pageWidth, (int) headerHeight);

    tableHeader.paint(g2); // draw the header on every page
    g2.translate(0, headerHeight);
    g2.translate(0, -tableHeightOnFullPage * rowIndex);

    // cut table image and draw on the page

    g2.setClip(pageLeft, (int) tableHeightOnFullPage * rowIndex, pageWidth,
        (int) clipHeight);
    jtBuildPlan.paint(g2);

    double pageTop = tableHeightOnFullPage * rowIndex - headerHeight;
    double pageBottom = pageTop + clipHeight + headerHeight;
    g2.drawRect(pageLeft, (int) pageTop, pageWidth,
        (int) (clipHeight + headerHeight));
  }

 
DanFlyBoyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Manikandan ThiagarajanSenior consultantCommented:
could you please post the full code?
Kelvin_KingCommented:
You might want to try this free API for printing java SWING components:

http://www.wildcrest.com/Software/J2PrinterWorks/J2PrinterWorksREADME.html

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelvin_KingCommented:
BTW, in Java 1.5 JTable now has convenience methods for printing, check it out.

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTable.html
Kelvin_KingCommented:
In particular interest is the PrintMode, where you can specify FIT_WIDTH, to print on a single A4 size paper.

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTable.PrintMode.html
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.