Jtable.print();

2 respostas
L

Pessoal, o java 5 tem um novo recurso para impressão de uma tabela.

Eu estou a usar a instrução : jtable.print(); e consigo imprimir a minha tabela perfeito.

Agora preciso de mais duas coisas: colocar um titulo na minha tabela e colocar o numero de paginas que foram imprimidas( tipo 1 de 10 ).

Eu sei que isso é possivel, pois o SwingSet2.jar ( D:\Program Files\Java\jdk1.5.0_04\demo\jfc\SwingSet2) que vem com o java tras um exemplo de uma tabela aonde tem estas duas funcionalidades.

Estive a ver o codigo e perdi-me um pouco, e gostaria que alguem me ajudase com um exemplo simples.

Obrigado desde ja !

2 Respostas

ciczan

Pelo que vi você vai ter que usar o método getPrintable da classe JTable. No javadoc desse método tem bem explicado como usar. Algo como:

// prepare the table for printing here first (for example, hide selection)

     // wrap in a try/finally so table can be restored even if something fails
     try {
         // fetch the printable
         Printable printable = table.getPrintable(JTable.PrintMode.FIT_WIDTH,
                                                  new MessageFormat("My Table"),
                                                  new MessageFormat("Page - {0}"));

         // fetch a PrinterJob
         PrinterJob job = PrinterJob.getPrinterJob();

         // set the Printable on the PrinterJob
         job.setPrintable(printable);

         // create an attribute set to store attributes from the print dialog
         PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();

         // display a print dialog and record whether or not the user cancels it
         boolean printAccepted = job.printDialog(attr);

         // if the user didn't cancel the dialog
         if (printAccepted) {
             // do the printing (may need to handle PrinterException)
             job.print(attr);
         }
     } finally {
         // restore the original table state here (for example, restore selection)
     }
L

Pessoal vejam o codigo, o erro que me dá é o seguinte : unreported exception java.awt.print.PrinterException; must be caught or declared to be thrown.
O que devo fazer para resolver isso e imprimir a minha tabela ?

try {
      
      Printable printable = table.getPrintable(JTable.PrintMode.FIT_WIDTH,
                                               new MessageFormat("My Table"),
                                               new MessageFormat("Page - {0}"));

      
      PrinterJob job = PrinterJob.getPrinterJob();

     
      job.setPrintable(printable);


      PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();

      // display a print dialog and record whether or not the user cancels it
      boolean printAccepted = job.printDialog(attr);

      // if the user didn't cancel the dialog
      if (printAccepted) {
          // do the printing (may need to handle PrinterException)

job.print(attr); //ERRO : unreported exception java.awt.print.PrinterException; must be caught or declared to be thrown

}
  } finally {

  }
Criado 1 de maio de 2006
Ultima resposta 1 de mai. de 2006
Respostas 2
Participantes 2