Jtable未打印(打印空白文档)

时间:2015-07-11 05:26:42

标签: java jtable

Jtable table=somevalues;    table.print(JTable.PrintMode.FIT_WIDTH,null,null,true,null,true,null);

打印对话框正在打开但是当我打印表格时不打印只打印空白文档。有人能告诉我哪里出错了吗? 我有**编辑过的**和带有值的代码是

String data[][]=new String[1][9];
                    String head[]=new String[9];

                    head[0]="Date";
                    head[1]="Q1 Rate per piece";
                    head[2]="Q1 Total pieces";
                    head[3]="Q2 Rate per piece";
                    head[4]="Q2 Total pieces";
                    head[5]="Q3 Rate per piece";
                    head[6]="Q3 Total pieces";
                    head[7]="Total pieces";
                    head[8]="Total Amount";

                        data[0][0]="a";
                        data[0][1]="b";
                        data[0][2]="c";
                        data[0][3]="d";
                        data[0][4]="e";
                        data[0][5]="f";
                        data[0][6]="g";
                        data[0][7]="h";
                        data[0][8]="i";
                     JTable table=new JTable(data,head);
                boolean complete=table.print();

2 个答案:

答案 0 :(得分:0)

解决方案1 ​​

打印表的最简单方法是调用不带参数的print方法。请参阅下面的代码示例。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "savePicture" {

        //Image to be saved
        let newImage = self.appraisalPic.image

        //Store picture to PHAssets
        let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
        dispatch_async(dispatch_get_global_queue(priority, 0), {
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(newImage)
                let assetPlaceholder = createAssetRequest.placeholderForCreatedAsset
                let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset)
                albumChangeRequest.addAssets([assetPlaceholder])
                }, completionHandler: {(success, error)in
                    dispatch_async(dispatch_get_main_queue(), {
                        NSLog("Adding Image to Library -> %@", (success ? "Success":"Error!"))

                //Need code here which doesn't do segue until AFTER picture is saved
                    })
            })

        })
    }
}

当您调用没有参数的打印方法时,会显示一个打印对话框,然后以FIT_WIDTH模式交互式打印您的表格而不使用页眉或页脚。下面的代码示例显示了带有完整参数集的打印方法签名。

try {
    boolean complete = table.print();
    if (complete) {
        /* show a success message  */
        ...
    } else {
        /*show a message indicating that printing was cancelled */
        ...
    }
} catch (PrinterException pe) {
    /* Printing failed, report to the user */
    ...
}

当您使用所有参数调用print方法时,您明确选择打印功能,如打印模式,页眉和页脚文本,打印属性,目标打印服务,以及是否显示打印对话框,以及是以交互方式还是以非交互方式打印。要确定最适合您需求的参数,请参阅下面的可用功能说明。

JTable打印API提供以下功能:

以交互方式或非交互方式打印 显示打印对话框 将页眉或页脚(或两者)添加到打印版面 选择打印模式 自动布局和分页

解决方案2

以下是打印jtable的示例

boolean complete = table.print(JTable.PrintMode printMode,
                               MessageFormat headerFormat,
                               MessageFormat footerFormat, 
                               boolean showPrintDialog,
                               PrintRequestAttributeSet attr,
                               boolean interactive,
                               PrintService service);

答案 1 :(得分:0)

试试此代码

  public class TablePrint extends javax.swing.JFrame {

        /**
         * Creates new form TablePrint
         */
        public TablePrint() {
            initComponents();
        }

      @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

            jButtonPrint = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTableData = new javax.swing.JTable();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            jButtonPrint.setText("Print");
            jButtonPrint.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButtonPrintActionPerformed(evt);
                }
            });

            jTableData.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null}
                },
                new String [] {
                    "Name", "Age"
                }
            ));
            jScrollPane1.setViewportView(jTableData);

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(39, 39, 39)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButtonPrint)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(50, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jButtonPrint)
                    .addGap(0, 34, Short.MAX_VALUE))
            );

            pack();
        }// </editor-fold>                        

        private void jButtonPrintActionPerformed(java.awt.event.ActionEvent evt) {                                             
           try{
                jTableData.print();
            }catch(Exception ee){}
        }                                            

        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {

            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TablePrint().setVisible(true);
                }
            });
        }

        // Variables declaration - do not modify                     
        private javax.swing.JButton jButtonPrint;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTableData;
        // End of variables declaration                   
    }