問題描述
所以我有一個標準的 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 以打印表格格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!