JTable Druckfunktion anpassen

Aus Byte-Welt Wiki
Version vom 28. Februar 2009, 23:45 Uhr von AndreUhres (Diskussion | Beiträge) (Die Seite wurde neu angelegt: Dieses Beispiel zeigt, wie wir die JTable Print Funktion anpassen können. Das Beispiel druckt eine Überschrift mit zwei Zeilen und einem Rand. Siehe auch : http://jav...)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springenZur Suche springen

Dieses Beispiel zeigt, wie wir die JTable Print Funktion anpassen können. Das Beispiel druckt eine Überschrift mit zwei Zeilen und einem Rand. Siehe auch : http://java.sun.com/docs/books/tutorial/uiswing/misc/printtable.html

<code=java>/*

* TableReportDemo.java
* This demo shows you how to customize the JTable print function.
* As an example, we print a report header with two rows and a border.
*/

import java.awt.*; import java.awt.event.*; import java.awt.print.*; import java.util.logging.*; import javax.print.attribute.*; import javax.swing.*; import javax.swing.JTable.*; import javax.swing.table.*; public class TableReportDemo extends JFrame {

   private JTable table;
   private JButton btPrint;
   private Action printAction = new AbstractAction("Print...") {
       public void actionPerformed(final ActionEvent e) {
           try {
               printJTable();
           } catch (PrinterException ex) {
               Logger.getLogger(TableReportDemo.class.getName()).log(Level.SEVERE, null, ex);
           }
       }
   };
   public TableReportDemo() {
       super("TableReportDemo");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       btPrint = new JButton();
       add(btPrint, BorderLayout.SOUTH);
       btPrint.setAction(printAction);
       add(new JScrollPane(createTable()), BorderLayout.CENTER);
       pack();
   }
   public JTable createTable() {
       String[] title = new String[]{"Title A", "Title B", "Title C", "Title D", "Title E"};
       String[][] data = new String[][]{};
       DefaultTableModel model = new DefaultTableModel(data, title);
       table = new JTable(model);
       table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
       for (int i = 0; i < 50; i++) {
           model.addRow(new String[]{String.valueOf(i), "", "", ""});
       }
       return table;
   }
   private void printJTable() throws PrinterException {
       // possibly prepare the table for printing here first
       // wrap in a try/finally so table can be restored even if something fails
       try {
           // fetch the printable
           Printable printable = new TableReport(table);
           PrinterJob job = PrinterJob.getPrinterJob();// fetch a PrinterJob
           job.setPrintable(printable);// set the Printable on the PrinterJob
           // 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 (printAccepted) {// if the user didn't cancel the dialog
               job.print(attr);// do the printing
           }
       } finally {
           // possibly restore the original table state here
       }
   }
   public static void main(final String[] args) {
       Runnable gui = new Runnable() {
           @Override
           public void run() {
               try {
                   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
               } catch (Exception ex) {
                   ex.printStackTrace();
               }
               new TableReportDemo().setVisible(true);
           }
       };
       //GUI must start on EventDispatchThread:
       SwingUtilities.invokeLater(gui);
   }

} class TableReport implements Printable {

   private Printable tablePrintable;
   private PageFormat pageFormatJTable;
   public TableReport(final JTable table) {
       tablePrintable = table.getPrintable(JTable.PrintMode.FIT_WIDTH, null, null);
   }
   public int print(final Graphics graphics, final PageFormat pageFormat,
           final int pageIndex) throws PrinterException {
       Graphics2D g = (Graphics2D) graphics;
       int x1 = (int) pageFormat.getImageableX();
       int y1 = (int) pageFormat.getImageableY();
       int w1 = (int) pageFormat.getImageableWidth();
       int h1 = (int) pageFormat.getImageableHeight();
       if (pageFormatJTable == null) {
           pageFormatJTable = (PageFormat) pageFormat.clone();
           Paper paperJTable = pageFormatJTable.getPaper();
           if(pageFormatJTable.getOrientation() == PageFormat.PORTRAIT){
               paperJTable.setImageableArea(x1, y1 + 60,//skip header area
                       w1, h1 - 90);//reserve space for header and footer
           }else{
               paperJTable.setImageableArea(y1 + 60, x1,//skip header area
                       h1 - 90, w1);//reserve space for header and footer
           }
           pageFormatJTable.setPaper(paperJTable);
       }
       String title = "Title";
       String subtitle = "Subtitle";
       Font f = g.getFont();
       g.setFont(g.getFont().deriveFont(15f));
       FontMetrics fm = g.getFontMetrics();
       g.drawString(title, x1 + (w1-fm.stringWidth(title))/2, y1 + 15);
       g.setFont(f);
       fm = g.getFontMetrics();
       g.drawString(subtitle, x1 + (w1-fm.stringWidth(subtitle))/2, y1 + 30);
       g.drawRect(x1, y1, w1, 40);
       String footer = "Page " + (pageIndex + 1);
       g.drawString(footer, x1 + (w1-fm.stringWidth(footer))/2, y1 + h1 - 10);
       //print the table:
       Graphics gCopy = g.create();
       int retVal = tablePrintable.print(gCopy, pageFormatJTable, pageIndex);
       gCopy.dispose();
       //
       return retVal;
   }

}</code=java>