Dean
Dean

Reputation: 8998

JTable appearing as box when printing

I have the following method which creates a JTable then prints it out by its appearing as a rectangle no the page with the header and footer.

public void printModules(){
    MessageFormat header = new MessageFormat("Modules " + new Date());
    MessageFormat footer = new MessageFormat("Created by Assignments Database");
    try {
        JTable jtModules = new JTable(new ModulesTableModel(Controller.getInstance().getModules()));
        jtModules.setShowHorizontalLines(true);
        jtModules.setShowVerticalLines(true);
        jtModules.setShowGrid(true);

        boolean complete = jtModules.print(JTable.PrintMode.NORMAL, header, footer, true, null, false, null);

        if(complete){
            System.out.println("Printed");
        } else{
            System.out.println("Printing Cancelled");
        }
    } catch (PrinterException e) {
        e.printStackTrace();
    }
}

What else is wrong? There is data in the table as one that is created from the same data is showing in one of the panels.

In my abstract table model I have implemented the following methods:

Is there any other methods that need to be created?

Upvotes: 1

Views: 180

Answers (2)

Dean
Dean

Reputation: 8998

You need to display the table in order to print it, so add it to a JFrame, then frame.setVisible(true); then frame.setVisible(false);
This will make it print.

Upvotes: 2

mKorbel
mKorbel

Reputation: 109823

JTable has very reduced support for printing, there are some descriptions about printing in the tutorials about JTable (inc. code example) and Printing

Upvotes: 2

Related Questions