下面的打印按钮显示打印机选择窗口,但它不打印任何内容......但JTable包含数据
print_button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try {
boolean complete = table2.print();
if (complete) {
JOptionPane.showMessageDialog(null, "Done printing");
} else {
JOptionPane.showMessageDialog(null, "printing.....");
}
} catch (PrinterException pe) {
}
}});
答案 0 :(得分:1)
您需要为JTable
提供尺寸才能打印出来:
table2.setSize(table2.getPreferredSize());
它有数据是正确的,但它需要有一个大小才能使打印工作。
答案 1 :(得分:0)
我得到了答案
print_button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
MessageFormat header = new MessageFormat("report printing");
MessageFormat footer = new MessageFormat("page{0,number,integer}");
try {
table1.print(JTable.PrintMode.NORMAL,header,footer);
} catch (PrinterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}});