To make this short, I want to print a table that is divided into sections (Columns 1 - 16 and Columns 17 up) and this is the code I have thought of. However, with this code, only the first part of the table will be printed with the other part being blank.
public void printing()
{
Book book = new Book();
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();
pf.setOrientation(PageFormat.LANDSCAPE);
JTable p_table = print_table(); //Arranges and gets the table to be printed
boolean printAccepted = job.printDialog();
if (printAccepted) {
try {
for(int x = 1; x < 16; x++)
{
p_table.getColumnModel().getColumn(x).setMinWidth(0);
p_table.getColumnModel().getColumn(x).setMaxWidth(0);
}
Printable printable1 = p_table.getPrintable(
JTable.PrintMode.FIT_WIDTH,
new MessageFormat("SCHEDULE"),
new MessageFormat("Page - {0}"));
book.append(printable1, pf, 100);
job.setPageable(book);
job.print();
for(int x = 1; x < 16; x++)
{
p_table.getColumnModel().getColumn(x).setMinWidth(80);
p_table.getColumnModel().getColumn(x).setMaxWidth(80);
}
for(int x = 16; x < p_table.getColumnCount(); x++)
{
p_table.getColumnModel().getColumn(x).setMinWidth(0);
p_table.getColumnModel().getColumn(x).setMaxWidth(0);
}
Printable printable = p_table.getPrintable(
JTable.PrintMode.FIT_WIDTH,
new MessageFormat("SCHEDULE"),
new MessageFormat("Page - {0}"));
book.append(printable, pf, 100);
job.setPageable(book);
job.print();
} catch (PrinterException e) {
System.out.println(e);
}
}
}
I tried duplicating the table using
JTable p_table = print_table();
JTable p_table = p_table;
//JTable p_table = new JTable(p_table.getModel());
However, this ended up making both tables blank since it editing the one table affects the other. And making a new table by copying the first table's table model also prints a blank table. Is there a better way of doing this?
Note that printing the table without dividing it works completely just fine.
To give you an idea of the output, refer to the pictures