[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.0

Printing JTComponents and JTable

Asked by totsubo in Java Programming Language

Tags: scale, print, double

Here are the specs:

1- Print out the contents of a JTable which may span multiple pages
2- At the top of the first page print out a JPanel

This should be easy but I'm just not understanding how to use translate() and clip().

Here's the code I have so far, but it isn't printing things properly at all ...

package TAL.TALObjects;

import TAL.Panels.*;
import java.awt.print.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class Preview extends javax.swing.JFrame implements Printable {
 
  private JTable table;
  private CustomerReceiptTopPanel topPanel;
  private int tableWidth = 0;
 
  public Preview(JTable table, CustomerReceiptTopPanel topPanel) {
    this.table    = table;
    this.topPanel = topPanel;
    setColumnWidths(table); //makes the tablecolumns as wide as needed
    initComponents();

    getContentPane().add(topPanel, new java.awt.GridBagConstraints());

    scrollPane.setViewportView(table);
    pack();
    setSize(tableWidth + 100, 800);
    setVisible(true);
  }
 
  public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
   
    Graphics2D g2 = (Graphics2D) g;
    double scale = 1;
    double topPanelYClip = 0;
    g2.setColor(Color.black);
   
    //how much room will the Header of the first page take?
    double topPanelHeight = topPanel.getHeight();

    //calculate the scaling factor (if needed)
    double pageHeight = pf.getImageableHeight();
    double pageWidth  = pf.getImageableWidth();
    double tableWidth = (double)table.getColumnModel().getTotalColumnWidth();
    if (tableWidth >= pageWidth) { scale =  pageWidth / tableWidth; }

    double headerHeightOnPage = table.getTableHeader().getHeight() * scale;
    double tableWidthOnPage   = tableWidth * scale;
   
    double oneRowHeight = (table.getRowHeight() + table.getRowMargin()) * scale;
    int numRowsOnAPage   = (int)((pageHeight - headerHeightOnPage) / oneRowHeight);
    double pageHeightForTable = oneRowHeight * numRowsOnAPage;

    int numRowsTakenByTopPanel = (int)Math.ceil(topPanelHeight / oneRowHeight);
    int numRowsOnFirstPage     = numRowsOnAPage - numRowsTakenByTopPanel;
    double pageHeightForTableOnFirstPage = oneRowHeight * numRowsOnFirstPage;
   
    int totalNumPages = (int)Math.ceil(((double)table.getRowCount() - numRowsOnFirstPage) / numRowsOnAPage) + 1;
    if(pageIndex >= totalNumPages) { return NO_SUCH_PAGE; }

    //move the pen to 0,0 of the page (takes into account margins)
    g2.translate(pf.getImageableX(), pf.getImageableY());

    //move the pen down below the header
    g2.translate(0f,headerHeightOnPage);
    //move the pen all the way back to 0,0
    if (pageIndex == 0) {}
    else g2.translate(0f,-(pageIndex - 1) * pageHeightForTable - pageHeightForTableOnFirstPage);
       
    if (pageIndex == 0) {
      //set clip for printing only the top panel
      g2.setClip(0,0, (int)topPanelHeight, (int)tableWidthOnPage);
      topPanel.print(g2);
    }
   
    //If this piece of the table is smaller than the size available,
    //clip to the appropriate bounds.
    if (pageIndex + 1 == totalNumPages) {
      int lastRowPrinted = numRowsOnAPage * pageIndex - numRowsTakenByTopPanel;
      int numRowsLeft = table.getRowCount() - lastRowPrinted;
      g2.setClip(0,
      (int)(pageHeightForTable * pageIndex + topPanelHeight),
      (int) Math.ceil(tableWidthOnPage),
      (int) Math.ceil(oneRowHeight * numRowsLeft));
    }
    //else clip to the entire area available.Unless this is the first page ...
    else{
      if (pageIndex == 0) {
        g2.setClip(0,
        (int)(topPanelHeight),
        (int) Math.ceil(tableWidthOnPage),
        (int) Math.ceil(pageHeightForTableOnFirstPage));
      }
      else {
        g2.setClip(0,
        (int)(pageHeightForTable * pageIndex),
        (int) Math.ceil(tableWidthOnPage),
        (int) Math.ceil(pageHeightForTable));
      }
    }
   
    g2.scale(scale,scale);
    table.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);
    table.getTableHeader().print(g2);
    //paint header at top
   
    return Printable.PAGE_EXISTS;
  }
 
  private void initComponents() {

    scrollPane = new javax.swing.JScrollPane();

    getContentPane().setLayout(new java.awt.GridBagLayout());
  }
}
 
Related Solutions
Keywords: Printing JTComponents and JTable
 
Loading Advertisement...
 
[+][-]11/18/02 12:59 AM, ID: 7462893Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/18/02 01:02 AM, ID: 7462898Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/18/02 03:59 AM, ID: 7463456Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/18/02 07:05 PM, ID: 7466978Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/01/03 07:31 PM, ID: 8250766Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04/01/03 07:39 PM, ID: 8250811Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/04/03 10:26 AM, ID: 8271408Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Java Programming Language
Tags: scale, print, double
Sign Up Now!
Solution Provided By: SpideyMod
Participating Experts: 2
Solution Grade: A
 
 
Loading Advertisement...
20091111-EE-VQP-89