java - 使用 swing 组件打印 JTable 和 JLabel?

标签 java swing printing jtable

我想使用 java swing 打印 JTable 和 JLabel。我尝试了下面的代码,它仅打印 JLabel 值。 JTable 不打印。

请帮忙。

代码:

public class TablePrintDemo1 extends JPanel implements Printable, ActionListener {

        private static final long serialVersionUID = 1L;
        static JFrame frameToPrint;
        JLabel l1, l2, l3, l4, l5, l6, l7, l8;
        JTextField t1, t2, t3, t4, t5;
        JButton printButton;
        DefaultTableModel model = new DefaultTableModel();
        JTable tabGrid = new JTable(model);
        JScrollPane scrlPane = new JScrollPane(tabGrid);
        static Vector<Object> tableValues = new Vector<Object>();
        static List<String> dataValues = new ArrayList<String>();

        public TablePrintDemo1(Vector<Object> tableValues,
                List<String> dataValues) {
            setLayout(null);
            setVisible(true);
            frameToPrint = new JFrame("Sample Table");
            l1 = new JLabel("Silver Shop ");
            l1.setBounds(100, 70, 550, 40);
            add(l1);

            Font f = new Font("Berlin Sans FB Demi", Font.BOLD, 40);
            l1.setFont(f);

            l2 = new JLabel(
                    "Address_Line1, Address_Line2, City, Pin Code.");
            l2.setBounds(40, 120, 550, 40);
            add(l2);

            l2.setFont(new Font("Courier New", Font.BOLD, 15));

            l3 = new JLabel("Bill Number ");
            l7 = new JLabel();

            l7.setFont(new Font("Courier New", Font.BOLD, 20));

            //      t1 = new JTextField(20);
            l3.setBounds(70, 180, 150, 20);
            l7.setBounds(150, 180, 120, 20);
            l7.setText(dataValues.get(0));
            add(l3);
            add(l7);

            l4 = new JLabel("Bill Date");
            l8 = new JLabel();
            l4.setBounds(300, 180, 200, 20);
            l8.setBounds(400, 180, 200, 20);
            l8.setText(dataValues.get(2));
            l7.setFont(new Font("Courier New", Font.BOLD, 12));

            add(l4);
            add(l8);

            model.addColumn("Product Name");
            model.addColumn("Quantity");
            model.addColumn("Price (1 gram)");
            model.addColumn("Total No.of grams");
            model.addColumn("Amount");
            scrlPane.setBounds(50, 220, 500, 350);
            frameToPrint.add(scrlPane);
            tabGrid.setFont(new Font("Latha", Font.PLAIN, 12));

            for (Object v : tableValues) {
                model.addRow((Vector) v);
                System.out.println("Object Values : " + v);
            }

            l5 = new JLabel("Total Amount");
            l5.setBounds(300, 570, 500, 20);
            l5.setFont(new Font("Times New Roman", 1, 20));
            add(l5);

            l6 = new JLabel();
            l6.setBounds(420, 570, 500, 20);
            l6.setFont(new Font("Times New Roman", 1, 20));
            l6.setText(dataValues.get(1));
            add(l6);

            printButton = new JButton("Print");
            printButton.addActionListener(this);
            frameToPrint.add("South", printButton);
            frameToPrint.getContentPane().add(this);
            tabGrid.setOpaque(false);
            printButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    printButton.setVisible(false);
                }
            });
        }

        /*@Override
        public void actionPerformed(ActionEvent e) {
            printButton.setVisible(false);
        }*/

        public static void main(String a[]) {
            dataValues.add("12");
            dataValues.add("760.00");
            dataValues.add("08/06/2015");           

            TablePrintDemo1 tablePrintDemo1 = new TablePrintDemo1(tableValues, dataValues);
            frameToPrint.add(tablePrintDemo1);
            frameToPrint.setSize(700, 680);
            frameToPrint.setBackground(Color.WHITE);
            frameToPrint.show();
        }

        public int print(Graphics g, PageFormat pf, int page)
                throws PrinterException {

            if (page > 0) { /* We have only one page, and 'page' is zero-based */
                return NO_SUCH_PAGE;
            }

            Graphics2D g2 = (Graphics2D) g;
            g2.translate(pf.getImageableX(), pf.getImageableY() - 55);

            AffineTransform originalTransform = g2.getTransform();

            double scaleX = pf.getImageableWidth() / this.getWidth();
            double scaleY = pf.getImageableHeight() / this.getHeight();
            // Maintain aspect ratio
            double scale = Math.min(scaleX, scaleY);
            g2.translate(pf.getImageableX(), pf.getImageableY());
            g2.scale(scale, scale);
            g2.setTransform(originalTransform);

            this.printAll(g2);

            /* tell the caller that this page is part of the printed document */
            return PAGE_EXISTS;
        }

        public void actionPerformed(ActionEvent e) {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(this);
            boolean ok = job.printDialog();
            if (ok) {
                try {
                    job.print();
                } catch (PrinterException ex) {
                    ex.printStackTrace();
                }
            }
        }

}

最佳答案

避免使用 null 布局,像素完美布局是现代 UI 设计中的一种幻觉。影响组件个体尺寸的因素太多,您无法控制其中任何一个。 Swing 的设计初衷是与布局管理器一起工作,放弃它们将导致无穷无尽的问题,您将花费越来越多的时间来尝试纠正

您已将 JScrollPane 添加到 JFrame...

frameToPrint.add(scrlPane);

然后将 JPanel 添加到同一个 JFrame

frameToPrint.getContentPane().add(this);

JScrollPane 渲染在 JPanel 上方有点侥幸,但是当您调用 this.printAll 时,它不会打印JScrollPane 因为它根本不包含在 JPanel 中。

我建议您查看 Jasper Reports,您甚至可以利用 JTablein built printing support directly相反,对于 example

关于java - 使用 swing 组件打印 JTable 和 JLabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30772092/

相关文章:

java - 与 ASCII 不同的编码,即使对于字母也是如此

java - 各种搜索算法的Big-O运行时间

java - 关闭框架的最佳方法?

220 毫秒的 android 打印和斑马票

python - 从 Tkinter 打印 Canvas

c# - 如何在 WinForms# 中打印面板?

java - 在JAVA(Eclipse)中序列化文件时如何将编码设置为UTF8

java - 每 X 秒运行一次代码 (Java)

java - 在 Swing 中切换面板和传递数据

具有多个操作的 Java 键绑定(bind)