i am a beginner type in using java JTable. I have a big problem.. I have to print the information needed like: name, address, time, Sex.. etc. before printing the Table's content. The print output must:
Name: ABCDEFGHI etc.. Department: Accounting
Address: ABCDEFGHI etc..... Location: Southern Loc.
Sex: Male
________________________________________________________
| Mothers' Name | Father's Name | School | Sport |
--------------------------------------------------------------------------------
1.
2.
3.
4.
5.
I tried the MessageFormat, But I don't think its a good Idea.
any help for this will be my chance to overcome my worry. thank you
this is my code:
================
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(this);
pj.printDialog();
try{
pj.print();
}catch (Exception PrintException) {}
======= method of print()
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)table.getColumnModel().getTotalColumnWidth();
// height of TEXT BEFORE JTable
double textHeight = 10f;
double scale = 1;
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 totalNumPages= (int)Math.ceil(((double)table.getRowCount())/numRowsOnAPage);
if(pageIndex>=totalNumPages)
{
return Printable.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 = table.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);
table.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);
table.getTableHeader().paint(g2);//paint header at top
return Printable.PAGE_EXISTS;
}
can u guide me a right coding for this.
This post has been edited by hiro52582: 07 March 2009 - 08:50 AM

New Topic/Question
Reply




MultiQuote




|