I tried doing a temporary JTable with the selected rows of the Main JTable and the same head and cell renders, but when I try to print it I just get an empty rectangle with a line border, I saw in How to print out specific rows/columns of a JTable? another way using rectangle and cell bounds, but I don't get it, I need some help to make it clearer
            Asked
            
        
        
            Active
            
        
            Viewed 2,718 times
        
    0
            
            
        - 
                    I assume you meant, you created a second `JTable`, with a `TableModel` which contained the selected rows from the master `JTable`... – MadProgrammer Oct 27 '14 at 03:55
 - 
                    yes, and it doesnt work – Jose Miguel Ledón Oct 27 '14 at 04:01
 
1 Answers
4
            Seems to work okay for me...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class TestPrint {
    public static void main(String[] args) {
        new TestPrint();
    }
    public TestPrint() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                DefaultTableModel model = new DefaultTableModel(0, 26);
                for (int row = 0; row < 26; row++) {
                    Vector data = new Vector(26);
                    for (int col = 0; col < 26; col++) {
                        String value = row + "x" + ((char) (col + 'A'));
                        data.add(value);
                    }
                    model.addRow(data);
                }
                JTable table = new JTable(model);
                JButton print = new JButton("Print");
                print.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            DefaultTableModel viewModel = (DefaultTableModel) table.getModel();
                            DefaultTableModel printModel = new DefaultTableModel(0, viewModel.getColumnCount());
                            for (int row : table.getSelectedRows()) {
                                printModel.addRow((Vector) viewModel.getDataVector().get(row));
                            }
                            JTable toPrint = new JTable(printModel);
                            toPrint.setSize(toPrint.getPreferredSize());
                            JTableHeader tableHeader = toPrint.getTableHeader();
                            tableHeader.setSize(tableHeader.getPreferredSize());
                            toPrint.print(JTable.PrintMode.FIT_WIDTH);
                        } catch (PrinterException ex) {
                            ex.printStackTrace();
                        }
                    }
                });
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.add(print, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
        MadProgrammer
        
- 343,457
 - 22
 - 230
 - 366
 
- 
                    your example works perfectly, I'll check what's the problem with mine, thanks a lot. – Jose Miguel Ledón Oct 27 '14 at 04:31
 - 
                    I "suspect" that the table needs to be resized before it's printed, but that's just the thought off the top of my head – MadProgrammer Oct 27 '14 at 04:33
 - 
                    yes, it was about the size, but now I have another problem, the printable version of the toPrint JTable is different from the main table printable, the width of all the columns is way smaller, I tried setting setPreferredWidth with the width of the each column, but it still give me small columns – Jose Miguel Ledón Oct 27 '14 at 05:05
 - 
                    See why a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem is so important...How are the column widths of your table within the GUI determined? – MadProgrammer Oct 27 '14 at 05:07
 - 
                    Ok thanks by the way, I was trying with preferredWidth but I changed it to setWidth and worked, thanks for your help. – Jose Miguel Ledón Oct 27 '14 at 05:17