I have a table called table
and its filled with data, I also have a MessageFormat
header I want to use as a header to print the JTable
this is the MessageFormat
:
MessageFormat header = new MessageFormat("Product: "
+ task.getProductName() + " Job: "
+ task.getJobNumber() + " Task: " + task.getTaskID()
);
I want to print 3 lines in the header, one for Product, Job and Task
the way I print this table
is like so:
table.print(JTable.PrintMode.FIT_WIDTH, header, null);
I can't seem to figure out how to print the header in 3 seperate lines, I tried using the \n
to make a new line but that doesn't seem to work.
I've utilized two
MessageFormat
arrays as a neat solution to the problem. You'll find below a printout of the end result:The code is outlined below:
You could try
If this doesn't work, then you're going to have to set up your own printer job, and layout the header precisely as you want it.
If you use the
getPrintable
method instead without adding a header/footer text, you can then include/decorate the returnedPrintable
in one where you have more control over the header, and where you can specify multi-line headers. See the javadoc of that method which mentionsI have not enough experience with
Printable
s to help you further on how to actually do thisBasically, the answer of @aymeric is correct: there's no way around a custom printable implementation. A way to do it with slightly less c&p is to have a custom implementation that
The trick in that approach is to fool the delegate tablePrintable into believing that the page is smaller than it actually is, with a custom pageFormat
more details (and code)
It's gonna be long answer (code wise) because the only solution I found was to implement a custom
Printable
. Of course I didn't write the following code myself, I mostly copied the code I extracted from the jdk sources and made some adjustments.Here we are:
This is the way you said you invoke the print method:
where
TablePrintable
is the following class (sorry for not being concise here):And here is the result (I hope that's what you expect):