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

查看:5

问题描述

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

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);

这使用一个临时的、屏幕外的 JTable 来进行实际打印,因此您需要确保配置任何所需的渲染器,但这个想法是合理的.

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.

此基本功能可确保屏幕上的 JTable 不会随着更改而更新,这可能会让用户感到害怕.

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.

它还允许您根据需要更改 TableHeader ;)

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

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