[英]How to print out specific rows/columns of a JTable?
我能够打印完整的JTable,但实际上我想更多地打印JTable的特定部分,例如从第10行到第50行以及第70列到第150列。如何做?
I've faced this problem too. 我也遇到过这个问题。 Solved by hiding columns before printing and restoring columns after printing:
通过在打印前隐藏列并在打印后恢复列来解决:
// get column num from settings
int num = gridSettings.getColumnsOnPage();// first <num> columns of the table will be printed
final TableColumnModel model = table.getColumnModel();
// list of removed columns. After printing we add them back
final List<TableColumn> removed = new ArrayList<TableColumn>();
int columnCount = model.getColumnCount();
// hiding columns which are not used for printing
for(int i = num; i < columnCount; ++i){
TableColumn col = model.getColumn(num);
removed.add(col);
model.removeColumn(col);
}
// printing after GUI will be updated
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// table printing
try {
table.print(PrintMode.FIT_WIDTH, null, null, true, hpset, true); // here can be your printing
} catch (PrinterException e) {
e.printStackTrace();
}
// columns restoring
for(TableColumn col : removed){
model.addColumn(col);
}
}
});
For printing your specific part of a JTable, just change a little bit this code. 要打印JTable的特定部分,只需更改此代码即可。
获取所选片段的单元格边界并计算所需区域( Rectangle
),定义剪辑区域以仅绘制所需区域,在可打印中使用Graphics's
translate()方法移动渲染。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.