获取JTable打印表格格式 [英] Get JTable to print tabular format

查看:125
本文介绍了获取JTable打印表格格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个标准的JTable,我希望用户能够打印出来。我使用了JTable.print(),打印出表格的确切副本就可以了,但是我希望能够以表格格式更多一些,只需要列名,然后是下面的数据,没有网格或任何东西。我以为这会很简单,但我不知道该怎么做!有没有人做到这一点?如果是这样,有人可以提供我的代码示例/示例?谢谢。

解决方案

您可以禁用网格线,例如...

  JTable printTable = new JTable(table.getModel()); 
printTable.setSize(printTable.getPreferredSize());
JTableHeader tableHeader = printTable.getTableHeader();
tableHeader.setSize(tableHeader.getPreferredSize());

printTable.setShowHorizo​​ntalLines(false);
printTable.setShowVerticalLines(false);
printTable.print(JTable.PrintMode.FIT_WIDTH);

它使用一个临时的离线 JTable 来做实际的打印,所以你需要确保配置任何所需的渲染器,但想法是健全的。

这基本确保 JTable 在屏幕上并没有被更新,这可能会让用户感到害怕。



它也允许你如果你想要更改 TableHeader ;)


So I have a standard JTable and I want the user to be able to print it out. I used JTable.print() and that's fine for printing out an exact replica of the table, but I was hoping to have it more in a tabular format, with just the column names and then the data beneath them, no grids or anything. I thought this would be simple, but I have no clue what to do! Has anybody done this? If so, can someone provide me with code sample/example? Thank you.

解决方案

You could disable the grid lines, for example...

JTable printTable = new JTable(table.getModel());
printTable.setSize(printTable.getPreferredSize());
JTableHeader tableHeader = printTable.getTableHeader();
tableHeader.setSize(tableHeader.getPreferredSize());

printTable.setShowHorizontalLines(false);
printTable.setShowVerticalLines(false);
printTable.print(JTable.PrintMode.FIT_WIDTH);

This uses a temporary, offscreen JTable to do the actually printing, so you will need to be sure to configure any required renderers, but the idea is sound.

This basic ensures that the JTable that is on the screen doesn't get updated with the changed, which could be kind of freaking to users.

It also allows you to change the TableHeader should you want to to ;)

这篇关于获取JTable打印表格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆