• Post Reply Bookmark Topic Watch Topic
  • New Topic

Print a non visible JTable  RSS feed

 
Francois Dereims
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody.
In this JTable, I've populated datas from my database. All i want is to print the table without displaying it but it doesn't work. What i get is the header and a blank box.
Here is a piece of my code:


Any idea would be helpfull. Thanks.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And where do you actually ADD something to the model? I doubt it would be in the modCandidats constructor, seeing as you provide an empty ArrayList.

Also, I'd consider renaming the model class to ModCandidats, with a capital M. That way you won't get confused with your field, also called ModCandidats.
 
Francois Dereims
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob. As I said, it's just a piece of code. My tmpList is not empty: the list is filled with Candidats objects via my ORM tool (iBatis), and i verify it with the condition if (!tmpList.isEmpty()).
So as the print job executes, i know my list contains data but they just don't appear in the printing !!!
The model class modCandidats is also ok: I use it elsewhere in the same program (but yes I can rename it as suggested).
Now i wonder if the JTable.print fonction doesn't require to display the JTable...
[ May 02, 2008: Message edited by: Francois Dereims ]
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done some testing with a DefaultTableModel, and I got the same result. Then I added the table to a JFrame and it worked.

Now I've done some more testing, but I still can't figure out why. I've called repaint() and revalidate() on the table but that doesn't work. Calling setVisible(true) also doesn't work; even an invisible table will get printed once added to a frame. Creating a JFrame just for printing seems so dumb though.


Edit: I did some more testing and found out that printComponent(Graphics) is not called at all. The reason is the size: it is 0x0. If this is the case, any JComponent will not paint or print itself. Once I called table.setSize(table.getPreferredSize()) I could print the table itself, without its header.
[ May 02, 2008: Message edited by: Rob Prime ]
 
Francois Dereims
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Rob for your testing.
Yes, create a frame just to print a JTable has no sens !!
I think I have no other choice than using a reporting tool (JasperReports for example) if i still want to hide this JTable.
Thanks again.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Once I called table.setSize(table.getPreferredSize()) I could print the table itself, without its header.

do the same for the table header, and the table/header prints out OK
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah of course. How stupid of me. The JTableHeader is just another JComponent. It will also only be added automatically if you add it to a JScrollPane - add the JTable to any other Container and it will miss the header until you add it yourself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!