Printing selected rows from JTable

后端 未结 1 715
有刺的猬
有刺的猬 2021-01-28 02:20

I\'m making a java swing application, I\'m also a newbie programer (1 and a half years programing).

I\'m trying to code inside a button a way to print (in a printer, not

相关标签:
1条回答
  • 2021-01-28 02:53

    You kind of missed the "real important" "trickery" part...

    JTable toPrint = new JTable(printModel);
    toPrint.setSize(toPrint.getPreferredSize());
    JTableHeader tableHeader = toPrint.getTableHeader();
    tableHeader.setSize(tableHeader.getPreferredSize());
    toPrint.print(JTable.PrintMode.FIT_WIDTH);
    

    It's really, really important that you set the size of the table and header

    Updated

    If you had basically copied the code from how to print selected rows JTable it would have basically worked

    Change...

    DefaultTableModel viewModel = (DefaultTableModel) jTable.getModel();
    DefaultTableModel printModel = new DefaultTableModel(viewModel.getRowCount(), viewModel.getColumnCount());
    JTable toPrint = new JTable(printModel);
    
    int[] selectedRows = jTable.getSelectedRows(); 
    for (int r=0;r<selectedRows.length;r++){
        toPrint.addRowSelectionInterval(r, r);
    }
    

    to

    DefaultTableModel viewModel = (DefaultTableModel) jTable.getModel();
    DefaultTableModel printModel = new DefaultTableModel(0, viewModel.getColumnCount());
    JTable toPrint = new JTable(printModel);
    
    for (int row : table.getSelectedRows()) {
        printModel.addRow((Vector)viewModel.getDataVector().get(row));
    }
    

    It prints the exact number of selected rows, but without the data. –

    So, I basically went back to how to print selected rows JTable, updated it to fix the "minor" bug so it would only print the selected rows, and run it without an issue...

    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 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);
                }
            });
        }
    
    }
    

    What I would recommend is that you start testing your code and make sure that the table is actually adding the rows you expect to the printModel

    0 讨论(0)
提交回复
热议问题