打印JTable时遇到问题

时间:2012-10-21 13:30:02

标签: java swing printing 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 个答案:

答案 0 :(得分:1)

  1. 为了更好地提供帮助,请发布SSCCE,因为您发布的非常类似的代码会打印出正确的输出(对于文件或纸张),没有人知道您的代码的重要部分是否发生了什么

    可能的解决方案_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _

  2. 没有将数组合并在一起,创建2D数组并将其设置为JTable(Object[][] rowData, Object[] columnNames) or JTable(String[][] rowData, String[] columnNames),对于测试无关紧要,确保Object[][]为各种数据类型做好准备(Double Date

  3. 中的String,而不仅仅是JTable}
  4. 所有更新必须在EventDispatchThread

  5. 上完成
  6. 必须在TopLevelContainer

  7. 上为新的InitialThread进行构建
  8. 对于上述两点,我们都会在invokeLater

  9. 中进行包装
  10. 请参阅JTables tutorial Printing,尝试使用代码示例(TablePrintDemo.java

  11. JTables教程以课程Printing

  12. 的链接结束