J_mie6
J_mie6

Reputation: 720

Having trouble printing a JTable

For my french verb conjugation program I trying to include an option to print a conjugation (in table form)

However the problem is that it prints a blank box, I have read some places that this is because the table is not been visible in the GUI or something like that. The problem is I want the table to be printed without adding it to the GUI at all...

the code for the table:

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;
}

and I want to print it with the following code:

                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));
            }

Does anybody have any idea what is going wrong here and how can I fix it?

Also as a side note, as this is a blank box I can't be sure but when it prints if a word is too long to fit in the cell will it get shorted to word... for example. And how can this be fixed?

Upvotes: 0

Views: 676

Answers (1)

mKorbel
mKorbel

Reputation: 109815

  1. For better help sooner, post an SSCCE, because very similair code your posted prints me correct output (to the File or paper), nobody know whats happened in important rest of your code

    possible solution___________________________________________________________

  2. without merging the Arrays together, create 2D array and put that as JTable(Object[][] rowData, Object[] columnNames) or JTable(String[][] rowData, String[] columnNames), doesn't matter for testing, sure Object[][] is prepared for various data types (Double, Date, e.i. not only the String) in JTable

  3. all updates must be done on EventDispatchThread

  4. building for a new TopLevelContainer must be done on InitialThread

  5. for both above mentioned points there is about wrapping in invokeLater

  6. see JTables tutorial Printing, try working code example ( TablePrintDemo.java )

  7. JTables tutorial ended with link to the lesson Printing

Upvotes: 1

Related Questions