Hi lkryss,

You should be able to put a text using drawString and move the JTable
down using translate.  Here is an example of a modified print method of
Report.java from
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/advprint.html#pe
to put a text before JTable.


 public int print(Graphics g, PageFormat pageFormat,
        int pageIndex) throws PrinterException {
       Graphics2D  g2 = (Graphics2D) g;
       g2.setColor(Color.black);
       int fontHeight=g2.getFontMetrics().getHeight();
       int fontDesent=g2.getFontMetrics().getDescent();

       //leave room for page number
       double pageHeight = pageFormat.getImageableHeight()-fontHeight;
       double pageWidth = pageFormat.getImageableWidth();
       double tableWidth = (double)
tableView.getColumnModel().getTotalColumnWidth();

       // height of TEXT BEFORE JTable
       double textHeight = 10f;

       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));//bottom
center

       //PUT TEXT ON TOP OF THE PAGE BEFORE JTABLE
        g2.drawString("TEXT before JTable",
                               (int)0, (int)textHeight);

       // MOVE TABLE USING TRANSLATE with added textHeight
       g2.translate(0f,headerHeightOnPage+textHeight);
       g2.translate(0f,-pageIndex*pageHeightForTable);

       //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;
                     int numRowsLeft = tableView.getRowCount() -
lastRowPrinted;
                     g2.setClip(0, (int)(pageHeightForTable * pageIndex),
                       (int) Math.ceil(tableWidthOnPage),
                       (int) Math.ceil(oneRowHeight * numRowsLeft));
       }
       //else clip to the entire area available.
       else{
                     g2.setClip(0, (int)(pageHeightForTable*pageIndex),
                     (int) Math.ceil(tableWidthOnPage),
                     (int) Math.ceil(pageHeightForTable));
       }

       g2.scale(scale,scale);
       tableView.paint(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().paint(g2);//paint header at top

       return Printable.PAGE_EXISTS;
   }


- Jennifer

[EMAIL PROTECTED] wrote:
hello
i try to print a jtable and some text before and after my jtable. printing the 
jtable is ok, and some text after too. but i don't really understand the code 
(took from 
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/advprint.html 
) i'd like to shift the jtable below some text.
someone has an idea please
(excuse my english )

    code:


        public int print(Graphics g, PageFormat pageFormat, int 
pageIndex)throws PrinterException
            {
                Graphics2D g2 = (Graphics2D)g;
                g2.setColor(Color.black);
                //taille de la police
                int fontHeight = g2.getFontMetrics().getHeight();
                System.out.println(fontHeight);
                int fontDescent = g2.getFontMetrics().getDescent();
                System.out.println(fontDescent );

                // laisser de l'espace pour le numero de page
                //hauteur de la page
                double pageHeight = pageFormat.getImageableHeight() - 
fontHeight;
                System.out.println("pageHeight"+pageHeight );
                //largeur  de la page
                double pageWidth = pageFormat.getImageableWidth();
                System.out.println("pageWidth"+pageWidth );
                //largeur du tableau
                double tableWidth = 
(double)jTable2.getColumnModel().getTotalColumnWidth();
                System.out.println("tableWidth "+tableWidth );
                double scale = 1;
                //si lengthtableau est plus large que la page, on redimensionne
                if (tableWidth >= pageWidth)
                {
                    scale = pageWidth / tableWidth;
                    System.out.println("scale "+scale );
                }
                double headerHeightOnPage = 
jTable2.getTableHeader().getHeight()*scale;
                System.out.println("headerHeightOnPage "+headerHeightOnPage );
                double tableWidthOnPage = tableWidth * scale;
                System.out.println("tableWidthOnPage "+tableWidthOnPage );
                double oneRowHeight = (jTable2.getRowHeight() + 
jTable2.getRowMargin())*scale;
                System.out.println("oneRowHeight "+oneRowHeight );
                int numRowsOnAPage = (int)((pageHeight-headerHeightOnPage) / 
oneRowHeight);
                System.out.println("numRowsOnAPage "+numRowsOnAPage);
                double pageHeightForTable = oneRowHeight * numRowsOnAPage;
                System.out.println("pageHeightForTable "+pageHeightForTable);
                int totalNumPages = 
(int)Math.ceil(((double)jTable2.getRowCount()) / numRowsOnAPage);
                if (pageIndex >= totalNumPages)
                    return NO_SUCH_PAGE;

                g2.translate(pageFormat.getImageableX()+50, 
pageFormat.getImageableY() +50); //(+50 decale a droite, + 50 decal en bas)
                // en bas au centre
                g2.drawString("Page: " + (pageIndex +1),(int)pageWidth/2 - 35, 
(int)(pageHeight + fontHeight - fontDescent));
                g2.translate(0f, headerHeightOnPage ); //  +50 ici decale la 
table vers le bas ???? touite la page vers le bas
                g2.translate(0f, -pageIndex*pageHeightForTable );// ici ca 
semble faire pareil que la ligne du dessus
                // si cette partie de la page est plus petite
                // que la taille disponible, alors découper les contours 
appropriés
                if (pageIndex + 1 == totalNumPages)
                {
                    int lastRowPrinted = numRowsOnAPage * pageIndex;
                    int numRowsLeft = jTable2.getRowCount() - lastRowPrinted;
                    //( , +50 coupe les premieres lignesdu tableau , , )
                    g2.setClip(0, (int)(pageHeightForTable * pageIndex 
),(int)(Math.ceil(tableWidthOnPage)),(int)Math.ceil(oneRowHeight * 
numRowsLeft));
                }
                // sinon découper la zone disponible toute entière
                else
                {//( , +50 coupe les premieres lignesdu tableau , , )
                    g2.setClip(0, (int)(pageHeightForTable * pageIndex 
),(int)(Math.ceil(tableWidthOnPage)),(int)Math.ceil(pageHeightForTable));
                }
                g2.scale(scale, scale);
                jTable2.paint(g2);
                // dessiner les entêtes en haut
                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));//( , , 
+50 aucun effet visible , )
                g2.scale(scale, scale);
                jTable2.getTableHeader().paint(g2);

                g2.drawString((String)jComboBox1.getSelectedItem(), (int) 
pageWidth / 2 - 100, (int) (fontHeight - fontDescent));
                return Printable.PAGE_EXISTS;
            }


Message was edited by: lkryss

[Message sent by forum member 'lkryss' (lkryss)]

http://forums.java.net/jive/thread.jspa?messageID=131622

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to