打印JTable的问题,怪!

雪里风火 2002-10-11 05:55:56
JTable的表头存放了一个TextField.
屏幕显示正常,表头是一个文本框.
执行打印Jtable的功能,出现怪事!
打印出来的还是完整的表格。表头没有被文本替换掉.
这是什么原因?
怎么解决?
高手赐招!!!!
...全文
78 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪里风火 2002-10-12
  • 打赏
  • 举报
回复
PrintSample文件:

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;

public class PrintSample extends JFrame {

protected JTable table;
//protected PageFormat pageFormat;

public static void main(String[] args) {
PrintSample ps = new PrintSample();
ps.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ps.pack();
ps.setVisible(true);
}

public PrintSample() {
super("Sample Print Application");
table = new JTable(new SampleTableModel());
table.setCellSelectionEnabled(false);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

//setContentPane(new JScrollPane(table));

JScrollPane jsp = new JScrollPane(table) {
public void setColumnHeaderView(Component comp) {
super.setColumnHeaderView(new JButton("This is a JButton"));
}
};
setContentPane(jsp);


//pageFormat = new PageFormat();
buildMenuBar();
}

protected void buildMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menu.add(new AbstractAction("Print",
new ImageIcon("print.gif")) {
public void actionPerformed(ActionEvent event) {
onPrint();
}
});
menu.addSeparator();
menu.add(new AbstractAction("Exit",
new ImageIcon("empty.gif")) {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
menuBar.add(menu);
setJMenuBar(menuBar);
}

protected void onPrint() {
Thread t = new Thread(new Runnable() {
public void run() {
PrinterJob pj = PrinterJob.getPrinterJob();
Paper paper = new Paper();
//paper.setImageableArea(72, 144, 6.5
PageFormat pageFormat = new PageFormat();
pageFormat.setPaper(paper);

TablePrinter tp = new TablePrinter(table);
pj.setPrintable(tp, pageFormat);

try {
pj.print();
} catch (PrinterException pe) {
JOptionPane.showMessageDialog(
PrintSample.this,
"Printing error:" +
pe.getMessage());
}
}
});
t.start();
}

}
//////////////////////////////////////////

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

public class TablePrinter implements Printable {

protected JTable table;

public TablePrinter(JTable tbl) {
table = tbl;
}

public int print(Graphics g, PageFormat pf, int index) {
if (index == 0) {
g.translate((int)(pf.getImageableX()),
(int)(pf.getImageableY()));
/* Graphics2D g2d = (Graphics2D)g;
double pageWidth = pf.getImageableWidth();
double pageHeight = pf.getImageableHeight();
double tableWidth = table.getWidth();
double tableHeight = table.getHeight();
// Find out what scale factor should be applied
// to make the table's width small enough to
// fit on the page
double scaleX = pageWidth / tableWidth;
// Now do the same for the height
double scaleY = pageHeight / tableHeight;
// Pick the smaller of the two values so that
// the table is as large as possible while
// not exceeding either the page's width or
// its height
double scaleFactor = Math.min(scaleX, scaleY);
// Now set the scale factor
g2d.scale(scaleFactor, scaleFactor);
*/ table.paint(g);
return Printable.PAGE_EXISTS;
}
return Printable.NO_SUCH_PAGE;
}

}
////////////////////////////////////
import javax.swing.table.*;

public class SampleTableModel extends AbstractTableModel {

public final static String[] columnNames = {"Name", "Address", "Phone",
"Postal Code", "Date"};

public final static Object[][] values = {
{"Janet Abel",
"1117 Sampson Street",
"3184397030", "70669-0531", "2009/10/3"},

{"Debbie Anderson",
"3420 Davis Road",
"3184941888", "70669-0645", "1999/7/18"},

{"Teri White",
"2516 Westwood Road",
"3184940263", "70669-0201", "2005/6/12"},

{"Janet Abel",
"1117 Sampson Street",
"3184397030", "70669-0531", "2009/10/3"},

{"Debbie Anderson",
"3420 Davis Road",
"3184941888", "70669-0645", "1999/7/18"},

{"Teri White",
"2516 Westwood Road",
"3184940263", "70669-0201", "2005/6/12"}
};

public int getRowCount() {
return values.length;
}

public int getColumnCount() {
return 5;
}

public Object getValueAt(int row, int column) {
return values[row][column];
}

public String getColumnName(int column) {
return columnNames[column];
//return null;
}

}
这是我的历程,大家帮助看看

zrhk 2002-10-12
  • 打赏
  • 举报
回复
能给我一份代码吗?
我最近也被JTable搞得乱乱的
我的邮箱:zrhk@21cn.com
谢谢

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧