1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.text.MessageFormat;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class Main{
private static JTable table;
private static String h;
public static void main(String args[]) {
JFrame frame = new JFrame("Table Printing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int rows = 10;
int cols = 5;
table = new JTable(rows, cols);
table.setRowHeight(30);
table.getColumn(0).setWidth(300);
table.getColumn(1).setWidth(300);
table.getColumn(2).setWidth(300);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("Print");
ActionListener printAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
MessageFormat headerFormat = new MessageFormat("Page {0}");
MessageFormat footerFormat = new MessageFormat("- {0} -");
table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
} catch (PrinterException pe) {
System.err.println("Error printing: " + pe.getMessage());
}
}
};
button.addActionListener(printAction);
frame.add(button, BorderLayout.SOUTH);
frame.setSize(300, 150);
frame.setVisible(true);
}
// Set the height of all rows to 32 pixels high,
// regardless if any heights were assigned to particular rows
// the height of the 1st row is set to 32 pixels high
// Returns the preferred height of a row.
// The result is equal to the tallest cell in the row.
public int getPreferredRowHeight(JTable table, int rowIndex, int margin) {
// Get the current default height for all rows
int height = table.getRowHeight();
// Determine highest cell in the row
for (int c=0; c<table.getColumnCount(); c++) {
TableCellRenderer renderer = table.getCellRenderer(rowIndex, c);
Component comp = table.prepareRenderer(renderer, rowIndex, c);
int h = comp.getPreferredSize().height + 2*margin;
height = Math.max(height, h);
}
return height;
}
// The height of each row is set to the preferred height of the
// tallest cell in that row.
public void packRows(JTable table, int margin) {
packRows(table, 0, table.getRowCount(), margin);
}
// For each row >= start and < end, the height of a
// row is set to the preferred height of the tallest cell
// in that row.
public void packRows(JTable table, int start, int end, int margin) {
for (int r=0; r<table.getRowCount(); r++) {
// Get the preferred height
int h = getPreferredRowHeight(table, r, margin);
// Now set the row height using the preferred height
if (table.getRowHeight(r) != h) {
table.setRowHeight(r, h);
}
}
}
} |
Partager