使用swing组件打印JTable和JLabel?

时间:2015-06-11 05:14:24

标签: 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();
                }
            }
        }

}

1 个答案:

答案 0 :(得分:1)

避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正

您已将JScrollPane添加到JFrame ...

frameToPrint.add(scrlPane);

然后将JPanel添加到同一JFrame

frameToPrint.getContentPane().add(this);

JScrollPane已在JPanel上方呈现,这是一种侥幸,但当您致电this.printAll时,它不会打印JScrollPane,因为它不包含在JPanel内完全是JTable

我建议您查看Jasper Reports,您甚至可以使用data.txt的{​​{3}}代替in built printing support directly