从JTable打印选定的行

时间:2016-01-12 11:15:36

标签: java swing printing jtable

我正在制作一个java swing应用程序,我也是一个新手程序员(1年半的编程)。

我正在尝试在按钮内编码一种方式来打印(在打印机中,而不是在控制台中)jTable的预选行(由用户),该jTable已经通过查询打印(在窗口中)

这是我的代码(在按钮内),我知道它是故事。我想,这样做的方法是在一个临时jTable中复制所有选中的行,然后打印temp jTable,但我无法实现这一点。

    MessageFormat header = new MessageFormat("- {TABLE} -"); 
        try {
            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);
            }


            toPrint.print(JTable.PrintMode.FIT_WIDTH,header,null);

        } catch (java.awt.print.PrinterException e1) {
             System.err.format("Cannot print %s%n", e1.getMessage()); 
        }

总而言之,我只想打印(在打印机中)现有jTable的选定行。

我也试过这个 - &gt; (how to print selected rows JTable)和其他可能的解决方案,但我不能让它们为我工作。

1 个答案:

答案 0 :(得分:2)

你错过了真正重要的&#34; &#34;诡计&#34;部分...

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

设置表格和标题

的大小真的非常重要

<强>更新

如果您基本上从how to print selected rows JTable复制了代码,那么基本上可以使用

更改...

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

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

它打印所选行的确切数量,但没有数据。 -

所以,我基本上回到how to print selected rows JTable,更新它以修复&#34;未成年人&#34; bug所以它只打印选定的行,并运行它没有问题......

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

}

我建议您开始测试代码,并确保该表实际上是在为printModel

添加您期望的行