JTable: print(PrintMode p, MessageFormat hFormat, MessageFormat fFormat)
import java.awt.BorderLayout;
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.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MainClass {
public static void main(String args[]) {
String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
String headers[] = { "Upper", "Lower" };
JFrame frame = new JFrame("Table Printing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTable table = new JTable(rows, headers);
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);
}
}
Related examples in the same category
1. | TableHeader.cellBorder | | |
2. | Table.scrollPaneBorder | | |
3. | JTable.AUTO_RESIZE_ALL_COLUMNS | | |
4. | JTable.AUTO_RESIZE_LAST_COLUMN | | |
5. | JTable.AUTO_RESIZE_NEXT_COLUMN | | |
6. | JTable.AUTO_RESIZE_OFF | | |
7. | JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS | | |
8. | new JTable(Object name, Object[][] data) | | |
9. | new JTable(TableModel dm) | | |
10. | new JTable(Vector rowData, Vector columnNames) | | |
11. | JTable: addColumnSelectionInterval(int index0, int index1) | | |
12. | JTable: changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) | | |
13. | JTable: clearSelection() | | |
14. | JTable: configureEnclosingScrollPane() 'Headerless Table' | | |
15. | JTable: createDefaultColumnsFromModel() | | |
16. | JTable: getCellSelectionEnabled() | | |
17. | JTable: getColumnCount() | | |
18. | JTable: getColumnModel() | | |
19. | JTable: getColumnSelectionAllowed() | | |
20. | JTable: getIntercellSpacing() | | |
21. | JTable: getRowCount() | | |
22. | JTable: getRowSelectionAllowed() | | |
23. | JTable: getSelectedColumns() | | |
24. | JTable: getSelectedRows() | | |
25. | JTable: getSelectionModel() | | |
26. | JTable: getTableHeader() | | |
27. | JTable: getValueAt(int row, int column) | | |
28. | JTable: isCellSelected(int row, int column) | | |
29. | JTable: moveColumn(int column, int targetColumn) | | |
30. | JTable: print() | | |
31. | JTable: removeColumn(TableColumn aColumn) | | |
32. | JTable: removeColumnSelectionInterval(int index0, int index1) | | |
33. | JTable: removeRowSelectionInterval(int index0, int index1) | | |
34. | JTable: selectAll() | | |
35. | JTable: setAutoCreateColumnsFromModel(boolean autoCreateColumnsFromModel) | | |
36. | JTable: setAutoResizeMode(int m) | | |
37. | JTable: setCellSelectionEnabled(boolean cellSelectionEnabled) | | |
38. | JTable: setColumnSelectionAllowed(boolean columnSelectionAllowed) | | |
39. | JTable: setDefaultEditor(Class columnClass, TableCellEditor editor)
| | |
40. | JTable: setDefaultRenderer(Class columnClass, TableCellRenderer renderer) | | |
41. | JTable: setFocusable(boolean focusable) | | |
42. | JTable: setGridColor(Color gridColor) | | |
43. | JTable: setIntercellSpacing(Dimension intercellSpacing) | | |
44. | JTable: setPreferredScrollableViewportSize(Dimension size) | | |
45. | JTable: setRowHeight(int rowHeight) | | |
46. | JTable: setRowSelectionAllowed(boolean rowSelectionAllowed) | | |
47. | JTable: setRowSelectionInterval(int index0, int index1) | | |
48. | JTable: setRowSorter(RowSorter extends TableModel> arg0) | | |
49. | JTable: setSelectionMode(int m) | | |
50. | JTable: setShowGrid(boolean showGrid) | | |
51. | JTable: setShowHorizontalLines(boolean showHorizontalLines) | | |
52. | JTable: setTableHeader(JTableHeader tableHeader) | | |
53. | JTable: setShowVerticalLines(boolean showVerticalLines) | | |
54. | JTable: setValueAt(Object aValue, int row, int column) | | |