Get JTable to print tabular format(获取 JTable 以打印表格格式)
问题描述
所以我有一个标准的 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 ;)
这篇关于获取 JTable 以打印表格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取 JTable 以打印表格格式
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01