user3736073
user3736073

Reputation: 25

Get JTable to print tabular format

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.

Upvotes: 0

Views: 302

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

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

Upvotes: 1

Related Questions