Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
private PrintRequestAttributeSet attr;
...
private void print() {
if (attr == null) {
attr = new HashPrintRequestAttributeSet();
float leftMargin = 10;
float rightMargin = 10;
float topMargin = 10;
float bottomMargin = 10;
attr.add(OrientationRequested.PORTRAIT);
attr.add(MediaSizeName.ISO_A4);
MediaSize mediaSize = MediaSize.ISO.A4;
float mediaWidth = mediaSize.getX(Size2DSyntax.MM);
float mediaHeight = mediaSize.getY(Size2DSyntax.MM);
attr.add(new MediaPrintableArea(
leftMargin, topMargin,
(mediaWidth - leftMargin - rightMargin),
(mediaHeight - topMargin - bottomMargin), Size2DSyntax.MM));
}
try {
jTable1.print(JTable.PrintMode.FIT_WIDTH, null, null, true, attr, true);
} catch (PrinterException ex) {
ex.printStackTrace();
} catch (HeadlessException ex) {
ex.printStackTrace();
}
}
Dazu dient der zweite und dritte Parameter (MessageFormat):Wie kann man ein Kopfzeile und Fusszeile setzen?
print(JTable.PrintMode, MessageFormat, MessageFormat, boolean, PrintRequestAttributeSet, boolean)
Folgende Methode ist verfügbar für erweiterte Druckanforderungen:Wie kann man in Kopfzeile ein multi line text schreiben?
getPrintable(JTable.PrintMode, MessageFormat, MessageFormat)
/*
* 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;
}
}