Results 1 to 3 of 3
  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    135
    Rep Power
    0

    Default JTable output to printer

    Hi

    I am trying to output the content of a JTable to printer.

    Heres my code:
    Java Code:
    private void cmdPrintActionPerformed(java.awt.event.ActionEvent evt) {                                         
            
            try 
                {
                    PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
                    set.add(OrientationRequested.LANDSCAPE);
                    vendTransOpen.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                    vendTransOpen.setModel(new Classes.VendTransOpenGrid());
                    vendTransOpen.setSize(vendTransOpen.getColumnModel().getTotalColumnWidth(), vendTransOpen.getRowHeight() * vendTransOpen.getRowCount());
                    MessageFormat headerFormat = new MessageFormat("Open transactions ");
                    MessageFormat footerFormat = new MessageFormat("- {0} -");
                    vendTransOpen.print(JTable.PrintMode.NORMAL, headerFormat, footerFormat, true, set, true);
                } 
            
            catch (PrinterException pe) {
                        System.err.println("Error printing: " + pe.getMessage());
            }
            
            vendTransOpen.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            
        }
    This works pretty nice, however I have a problem with the table and column size. I would like it to be the same size as I have on my screen. Any suggestions on how I can achieve this?

  2. #2
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: JTable output to printer

    What happens if you call setMinWidth(somenumber) or setPreferredWidth(somenumber)?

    With JTable.AUTO_RESIZE_OFF, the table will not change the size of any of the columns for you, so it will take your preferred setting. If it is your goal to have the columns default to your preferred size, except to have the last column fill the rest of the pane, You have the option of using the JTable.AUTO_RESIZE_LAST_COLUMN autoResizeMode, but it might be most effective when used with TableColumn.setMaxWidth() instead of TableColumn.setPreferredWidth() for all but the last column.

    Once you are satisfied that AUTO_RESIZE_LAST_COLUMN does in fact work, you can experiment with a combination of TableColumn.setMaxWidth() and TableColumn.setMinWidth()

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    135
    Rep Power
    0

    Default Re: JTable output to printer

    Thanks for the reply. The suggestion on using TableColumn.setMaxWidth() or TableColumn.setMinWidth() works fine, however a small issue:
    When I use this, the print is not on one page. The total size of the table too big, så the last column is on page 2. Is there a way to set the max size of the table to be inside the margins of the paper?

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2012, 05:44 PM
  2. Replies: 6
    Last Post: 01-30-2010, 03:55 PM
  3. Replies: 2
    Last Post: 02-20-2009, 08:25 AM
  4. Specifying a Network Printer in Printer Servlet
    By shapez in forum New To Java
    Replies: 0
    Last Post: 03-06-2008, 04:21 PM
  5. Replies: 0
    Last Post: 08-09-2007, 07:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •