首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何打印出JTable的特定行/列?

如何打印出JTable的特定行/列?
EN

Stack Overflow用户
提问于 2013-02-21 21:13:21
回答 2查看 2.2K关注 0票数 1

我可以打印完整的JTable,但实际上我希望打印JTable的特定部分,例如,从第10行打印到第50行,从第70列打印到第150列。该怎么做呢?

EN

Stack Overflow用户

发布于 2013-12-26 18:31:38

我也遇到过这个问题。解决方法:打印前隐藏列,打印后恢复列:

代码语言:javascript
运行
复制
// 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);
        }
    }
});

要打印JTable的特定部分,只需稍微修改一下代码即可。

票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15003159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档