很久以前参考的一个例子
如果是b/s结构的系统,这种打印方式不太容易实现
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;public class JTableTest extends JFrame{ final static Button button = new Button("Print"); public static void main(String args[]) {
JTableTest Test = new JTableTest();
Test.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Test.setSize(300,200);
Test.setVisible(true);
}
public JTableTest() { button.addActionListener(this);
JTable jt = new JTable(new MyTableModel());
//MyTable jt = new MyTable(new MyTableModel());
jt.setRowHeight(20);
jt.setCellSelectionEnabled(false);
getContentPane().add(new JScrollPane(jt));
TablePrinter tabeprint = new TablePrinter(jt);
tabeprint.print();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof Button) {
if(e.getSource() == Button) {
PrinterJob printJob = PrinterJob.getPrinterJob();
PrintJob.setPrintable(this);
//Book book = new Book ();
//book.append (new ShapesPrint(), printJob.defaultPage());
//book.append (new ShapesPrint(), printJob.defaultPage());
//printJob.setPageable (book); if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
}
}
}
}
/*
class MyTable extends JTable {
public MyTable(TableModel model){
super(model);
setUI(new MultiSpanCellTableUI());
setCellSelectionEnabled(false);//设置单元格为不可选
getTableHeader().setReorderingAllowed(false);//设置是否可以修改列头和列
setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
}
}
*/
class MyTableModel extends AbstractTableModel {
private String[] columns = {"姓名","年龄","已婚"};
private Object[][] data =
{{"王晓明asfsdfsdfsdf",new Integer(28),new Boolean(true)},
{"程晓华",new Integer(26),new Boolean(false)},
{"张小李",new Integer(35),new Boolean(true)},
{"林晓力",new Integer(18),new Boolean(false)}
};
public int getColumnCount() {
return (columns.length);
}
public int getRowCount() {
return (data.length);
}
public String getColumnName(int col) {
return null;
}
public Object getValueAt(int row, int col) {
return (data[row][col]);
}
public Class getColumnClass(int col) {
return (getValueAt(0,col).getClass());
}
public boolean isCellEditable( int row,int col) {
return (col > 0 );
}
public void setValueAt(Object value, int row, int col) {
if(col == 1) {
try {
data[row][col] = new Integer((String)value);
}
catch (NumberFormatException e) {
}
}else
data[row][col] = value;
fireTableCellUpdated(row,col);
}
}
如果是b/s结构的系统,这种打印方式不太容易实现
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;public class JTableTest extends JFrame{ final static Button button = new Button("Print"); public static void main(String args[]) {
JTableTest Test = new JTableTest();
Test.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Test.setSize(300,200);
Test.setVisible(true);
}
public JTableTest() { button.addActionListener(this);
JTable jt = new JTable(new MyTableModel());
//MyTable jt = new MyTable(new MyTableModel());
jt.setRowHeight(20);
jt.setCellSelectionEnabled(false);
getContentPane().add(new JScrollPane(jt));
TablePrinter tabeprint = new TablePrinter(jt);
tabeprint.print();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof Button) {
if(e.getSource() == Button) {
PrinterJob printJob = PrinterJob.getPrinterJob();
PrintJob.setPrintable(this);
//Book book = new Book ();
//book.append (new ShapesPrint(), printJob.defaultPage());
//book.append (new ShapesPrint(), printJob.defaultPage());
//printJob.setPageable (book); if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
}
}
}
}
/*
class MyTable extends JTable {
public MyTable(TableModel model){
super(model);
setUI(new MultiSpanCellTableUI());
setCellSelectionEnabled(false);//设置单元格为不可选
getTableHeader().setReorderingAllowed(false);//设置是否可以修改列头和列
setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
}
}
*/
class MyTableModel extends AbstractTableModel {
private String[] columns = {"姓名","年龄","已婚"};
private Object[][] data =
{{"王晓明asfsdfsdfsdf",new Integer(28),new Boolean(true)},
{"程晓华",new Integer(26),new Boolean(false)},
{"张小李",new Integer(35),new Boolean(true)},
{"林晓力",new Integer(18),new Boolean(false)}
};
public int getColumnCount() {
return (columns.length);
}
public int getRowCount() {
return (data.length);
}
public String getColumnName(int col) {
return null;
}
public Object getValueAt(int row, int col) {
return (data[row][col]);
}
public Class getColumnClass(int col) {
return (getValueAt(0,col).getClass());
}
public boolean isCellEditable( int row,int col) {
return (col > 0 );
}
public void setValueAt(Object value, int row, int col) {
if(col == 1) {
try {
data[row][col] = new Integer((String)value);
}
catch (NumberFormatException e) {
}
}else
data[row][col] = value;
fireTableCellUpdated(row,col);
}
}
解决方案 »
- 求解!!!!
- struts2.1 <s:token />使用问题!
- 用Java实现网络语音信号传送
- Spring的声明式事务问题,请大家帮忙
- struts2 中<s:if 为什么有时候生效有时候无效?有人遇到过类似的情况么?
- 请问如何在java代码中判断当前所有的数据库是mssql还是oracle?
- 我有两张数据库表,它们有很多指标字段是相同的,我想把这两张表都实现成CMP的实体BEAN,可以吗?
- java com.sun.tools.xjc.Main ???
- weblogic上同步问题?高分相送
- ejb是sun对corba的可移植性和复杂性的解决方案
- rational rose注册
- 请教高手,访问数据库时出错?
大家有没有什么好办法啊?