Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing JTable tutorial needed & help

Status
Not open for further replies.

soldieryap

Programmer
Aug 15, 2000
59
MY
does anyone know where can i find good tutorial about printing JTable over the net ?

i have an example that can print out the table, but it won't print out the column that out of paper. i tried and have no idea how it return back to print out the remaining row that haven't been printed!! crazy soon ...

the code is below: (and what is System.gc() ?)

*********************************************************
public int print(Graphics pg, PageFormat pageFormat,
int pageIndex) throws PrinterException {
if (pageIndex >= m_maxNumPage)
return NO_SUCH_PAGE;

pg.translate((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY());
int wPage = 0;
int hPage = 0;
if (pageFormat.getOrientation() == pageFormat.PORTRAIT) {
wPage = (int)pageFormat.getImageableWidth();
hPage = (int)pageFormat.getImageableHeight();
}
else {
wPage = (int)pageFormat.getImageableWidth();
wPage += wPage/2;
hPage = (int)pageFormat.getImageableHeight();
pg.setClip(0,0,wPage,hPage);
}

int y = 0;
pg.setFont(m_title.getFont());
pg.setColor(Color.black);
Font fn = pg.getFont();
FontMetrics fm = pg.getFontMetrics();
y += fm.getAscent();
pg.drawString(m_title.getText(), 0, y);
y += 20; // space between title and table headers

Font headerFont = m_table.getFont().deriveFont(Font.BOLD);
pg.setFont(headerFont);
fm = pg.getFontMetrics();

TableColumnModel colModel = m_table.getColumnModel();
int nColumns = colModel.getColumnCount();
int x[] = new int[nColumns];
x[0] = 0;

int h = fm.getAscent();
y += h; // add ascent of header font because of baseline
// positioning

int nRow, nCol, pagePerRow = 1;
for (nCol=0; nCol<nColumns; nCol++) {
TableColumn tk = colModel.getColumn(nCol);
int width = tk.getWidth();
if (x[nCol] + width > wPage) {
nColumns = nCol;
break;
}
if (nCol+1<nColumns)
x[nCol+1] = x[nCol] + width;
String title = (String)tk.getIdentifier();
pg.drawString(title, x[nCol], y);
}

pg.setFont(m_table.getFont());
fm = pg.getFontMetrics();

int header = y;
h = fm.getHeight();
int rowH = Math.max((int)(h*1.5), 10);
int rowPerPage = (hPage-header)/rowH;
m_maxNumPage = Math.max((int)Math.ceil((m_table.getRowCount()/
(double)rowPerPage)), 1);

TableModel tblModel = m_table.getModel();
int iniRow = pageIndex*rowPerPage;
int endRow = Math.min(m_table.getRowCount(),
iniRow+rowPerPage);

for (nRow=iniRow; nRow<endRow; nRow++) {
y += h;
for (nCol=0; nCol<nColumns; nCol++) {
int col = m_table.getColumnModel().getColumn(nCol).getModelIndex();
Object obj = m_data.getValueAt(nRow, col);
String str = obj.toString();
if (obj instanceof ColorData)
pg.setColor(((ColorData)obj).m_color);
else
pg.setColor(Color.black);
pg.drawString(str, x[nCol], y);
}
}

System.gc();
return PAGE_EXISTS;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top