簡體   English   中英

無法打印JTable

[英]Having trouble printing a JTable

對於我的法語動詞變位程序,我試圖包括一個打印變位的選項(表格形式)

但是,問題在於它打印出一個空白框,我已經讀過一些地方,這是因為該表在GUI或類似內容中不可見。 問題是我希望完全不將其添加到GUI的情況下打印表格...

該表的代碼:

JTable getPrint(String Infinitive)
{
    String [] aPresent = fetchTense(Tense.PRESENT, getID(Infinitive)).split("\n");
    String [] aPerfect = fetchTense(Tense.PERFECT, getID(Infinitive)).split("\n");
    String [] aImperfect = fetchTense(Tense.IMPERFECT, getID(Infinitive)).split("\n");
    String [] aSimpleFuture = fetchTense(Tense.SIMPLE, getID(Infinitive)).split("\n");
    String [] aConditional = fetchTense(Tense.CONDITIONAL, getID(Infinitive)).split("\n");
    String [] aColumnNames = {"Pronoun", "Present"  , "Perfect"  , "Imperfect"  , "Simple Future" , "Conditional"  };
    String [][] aValues = {
                             {"Je"     , aPresent[0], aPerfect[0], aImperfect[0], aSimpleFuture[0], aConditional[0]},
                             {"Tu"     , aPresent[1], aPerfect[1], aImperfect[1], aSimpleFuture[1], aConditional[1]},
                             {"Il"     , aPresent[2], aPerfect[2], aImperfect[2], aSimpleFuture[2], aConditional[2]},
                             {"Elle"   , aPresent[3], aPerfect[3], aImperfect[3], aSimpleFuture[3], aConditional[3]},
                             {"On"     , aPresent[4], aPerfect[4], aImperfect[4], aSimpleFuture[4], aConditional[4]},
                             {"Nous"   , aPresent[5], aPerfect[5], aImperfect[5], aSimpleFuture[5], aConditional[5]},
                             {"Vous"   , aPresent[6], aPerfect[6], aImperfect[6], aSimpleFuture[6], aConditional[6]}, 
                             {"Ils"    , aPresent[7], aPerfect[7], aImperfect[7], aSimpleFuture[7], aConditional[7]},
                             {"Elles"  , aPresent[8], aPerfect[8], aImperfect[8], aSimpleFuture[8], aConditional[8]}
                          };
    JTable pTable = new JTable(aValues, aColumnNames);
    return pTable;
}

我想用以下代碼打印它:

                try 
            {
                JTable pTable = pGUI.getParser().getPrint("Aller");
                JFrame fix = new JFrame();
                fix.add(pTable);
                fix.setVisible(true);
                fix.setVisible(false);
                boolean bComplete = pTable.print(JTable.PrintMode.FIT_WIDTH, new MessageFormat(String.format("Conjugation of %s", "Aller")), new MessageFormat("Page {0}"));
                if (bComplete) 
                {
                    JOptionPane.showMessageDialog(pGUI, "Finished printing", "Printed", JOptionPane.INFORMATION_MESSAGE);
                } 
                else
                {
                    JOptionPane.showMessageDialog(pGUI, "Printing Cancelled", "Printing Cancelled", JOptionPane.WARNING_MESSAGE);
                }
            } 
            catch (PrinterException e) 
            {
                JOptionPane.showMessageDialog(pGUI, "An error has occured", "Printing Error", JOptionPane.ERROR_MESSAGE);
            }
            finally
            {
                pGUI.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }

是否有人知道這里出了什么問題,我該如何解決?

另外請注意,因為這是一個空白框,所以我不確定,但是當它打印時,如果單詞太長而無法容納在單元格中,它將與單詞短接……。 以及如何解決?

  1. 為了盡快獲得更好的幫助,請發布SSCCE ,因為您發布的代碼非常相似 ,可以向我輸出正確的輸出(到文件或紙張),沒有人知道代碼中其他重要部分的內容

    可能solution_ _ __ _ __ __ _ _ _ __ __ _ __ _ __ __ _ _ _ __ __ _ __ _ __ __ _ _ _ __ __ _ __ _ __ __ _ _ _ __

  2. 無需將Array合並在一起,創建2D數組,並將其作為JTable(Object [] [] rowData,Object [] columnNames)或JTable(String [] [] rowData,String [] columnNames)進行測試,確保Object[][]JTable各種數據類型( DoubleDate ,不僅是String )准備

  3. 所有更新必須在EventDispatchThread上完成

  4. 新的TopLevelContainer的構建必須在InitialThread完成

  5. 對於上述兩個要點,都涉及在invokeLater包裝

  6. 請參閱JTables教程“打印” ,嘗試工作代碼示例( TablePrintDemo.java

  7. JTables教程,鏈接到的教訓結束了印刷

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM