This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable printing - choosing columns and rows  RSS feed

 
Alexander Walker
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is it possible to print a JTable, while choosing the columns and rows to print?

Example: If I have 5 columns in my JTable. One of these columns are made up of checkboxes. I want to print only the rows which have a checkbox that is true. And I dont want to print the column with checkboxes in it.

Sun's How to Use Tables shows the kind of table I explained above. That page also have a printing example on the same table, however it prints the whole table, while it should not print the rightmost column, and only the rows that have a checkbox that is true.

I googled but did'nt find anything of what I want to do.

Thanks in advance for any help.
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if the following will work, but you could at least try.

Create a new JTable using the same model. Set up the JTable the same way, with renderers et all. Then remove the columns you don't want. Next, apply a TableRowSorter with a RowFilter that filters out those rows for which the checked value (Boolean) is false. If you would show this JTable it would be an exact copy of your table except with some rows and columns missing. You won't show it though, nor add it to any container. You just call print on it. This may not work if the JTable is not visible, but I think it will.

Because you use a shared model that you don't change for the new JTable it will show the same data. The removing of the columns and the filtering is done on a graphical level only; the model stays the same.
 
Alexander Walker
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

It sounds like a good way, but there is one problem. I tried creating a new table, that I did'nt add to a container, but it did'nt print anything. And now I tried setting the tables setVisible to false that is supposed to be shown in the JFrame, and it did'nt print anyting. My guess is that the table have to be visible inorder to print. More specific, the table did print, but only a border surrounding the table, but noting inside it.
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've checked, and the reason is that because both the table and its header have a size of 0x0. The following SSCCE shows how to solve that:
These two setSize statements give the table and its header a proper size.
 
Alexander Walker
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did'nt think of that. It worked great. I have'nt tried the thing with TableRowSorter. I have'nt used it before but it will probably work, and then removing the column you don't want to print. It seems to be the best way to do it. I am goning to try it, but I might not have time to do it for a while, since I am going away for about 1.5 weeks.

Thanks for the help .
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!