Created
June 8, 2022 02:05
-
-
Save RustyKnight/3394d1bf4ecda3e165f18975af0b9431 to your computer and use it in GitHub Desktop.
A "simple" example of making a print preview of a JTable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import java.awt.Color; | |
import java.awt.Component; | |
import java.awt.Dimension; | |
import java.awt.EventQueue; | |
import java.awt.Graphics2D; | |
import java.awt.GridBagConstraints; | |
import java.awt.GridBagLayout; | |
import java.awt.Insets; | |
import java.awt.Rectangle; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.image.BufferedImage; | |
import java.awt.print.PageFormat; | |
import java.awt.print.Paper; | |
import java.awt.print.Printable; | |
import java.awt.print.PrinterException; | |
import java.util.Random; | |
import javax.swing.ImageIcon; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTable; | |
import javax.swing.Scrollable; | |
import javax.swing.table.DefaultTableModel; | |
import javax.swing.table.JTableHeader; | |
import javax.swing.table.TableModel; | |
public class PrintPreviewJTable { | |
public static void main(String[] args) { | |
new PrintPreviewJTable(); | |
} | |
public PrintPreviewJTable() { | |
EventQueue.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
JFrame frame = new JFrame(); | |
frame.add(new MainPane()); | |
frame.pack(); | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
} | |
}); | |
} | |
public class MainPane extends JPanel { | |
private DefaultTableModel tableModel = new DefaultTableModel(new String[]{"", "Name", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}, 0) { | |
@Override | |
public Class<?> getColumnClass(int columnIndex) { | |
switch (columnIndex) { | |
case 0: | |
return Integer.class; | |
case 1: | |
return String.class; | |
case 2: | |
case 3: | |
case 4: | |
case 5: | |
case 6: | |
case 7: | |
return Boolean.class; | |
} | |
return Object.class; | |
} | |
}; | |
public MainPane() { | |
setLayout(new GridBagLayout()); | |
JTable leftTable = new JTable(tableModel); | |
reload(); | |
GridBagConstraints gbc = new GridBagConstraints(); | |
gbc.weightx = 1; | |
gbc.weighty = 1; | |
gbc.fill = GridBagConstraints.BOTH; | |
gbc.gridx = 0; | |
gbc.gridy = 0; | |
JButton printButton = new JButton("Print"); | |
printButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
try { | |
TablePrintPreview preview = new TablePrintPreview(tableModel); | |
JOptionPane.showMessageDialog(MainPane.this, preview, "Preview", JOptionPane.PLAIN_MESSAGE); | |
} catch (PrinterException ex) { | |
ex.printStackTrace(); | |
JOptionPane.showMessageDialog(MainPane.this, "Failed to generate print view", "Error", JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
}); | |
add(new JScrollPane(leftTable), gbc); | |
gbc.gridy++; | |
gbc.weightx = 1; | |
gbc.weighty = 0; | |
gbc.fill = GridBagConstraints.NONE; | |
gbc.insets = new Insets(32, 32, 32, 32); | |
add(printButton, gbc); | |
} | |
protected void reload() { | |
tableModel.setRowCount(0); | |
Random rnd = new Random(); | |
int row = 0; | |
for (String[] name : NAMES) { | |
Object[] rowData = new Object[7]; | |
rowData[0] = row; | |
rowData[1] = name[0] + " " + name[1]; | |
for (int index = 2; index < rowData.length; index++) { | |
rowData[index] = rnd.nextBoolean(); | |
} | |
tableModel.addRow(rowData); | |
row++; | |
} | |
} | |
} | |
public class TablePrintPreview extends JPanel { | |
public TablePrintPreview(TableModel model) throws PrinterException { | |
setLayout(new BorderLayout()); | |
Paper paper = new Paper(); | |
// A4 | |
float paperWidth = cmsToPixel(21.0f, 72); | |
float paperHeight = cmsToPixel(29.7f, 72); | |
float margin = cmsToPixel(1f, 72); | |
System.out.println(pixelToCms(36, 72)); | |
paper.setSize(paperWidth, paperHeight); | |
paper.setImageableArea(margin, margin, paperWidth - (margin * 2), paperHeight - (margin * 2)); | |
PageFormat pageFormat = new PageFormat(); | |
pageFormat.setPaper(paper); | |
pageFormat.setOrientation(PageFormat.PORTRAIT); | |
int imageWidth = (int) Math.ceil(paperWidth); | |
int imageHeight = (int) Math.ceil(paperHeight); | |
ContentPane contentView = new ContentPane(imageWidth, imageHeight); | |
JTable table = new JTable(model); | |
table.setShowGrid(true); | |
table.setGridColor(Color.LIGHT_GRAY); | |
table.setLocation(0, 0); | |
table.setSize(table.getPreferredSize()); | |
JTableHeader tableHeader = table.getTableHeader(); | |
tableHeader.setSize(tableHeader.getPreferredSize()); | |
Printable printable = table.getPrintable(JTable.PrintMode.FIT_WIDTH, null, null); | |
int pageIndex = 0; | |
boolean morePages = true; | |
do { | |
BufferedImage preview = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB); | |
Graphics2D g2d = preview.createGraphics(); | |
g2d.setColor(Color.WHITE); | |
g2d.fillRect(0, 0, imageWidth, imageHeight); | |
g2d.setColor(Color.LIGHT_GRAY); | |
g2d.drawRect(0, 0, imageWidth - 1, imageHeight - 1); | |
morePages = printable.print(g2d, pageFormat, pageIndex) == printable.PAGE_EXISTS; | |
g2d.dispose(); | |
if (morePages) { | |
contentView.addPreview(new PagePreviewPane(preview)); | |
} | |
pageIndex++; | |
} while (morePages); | |
add(new JScrollPane(contentView)); | |
} | |
public static final float CM_PER_INCH = 0.393700787f; | |
public static final float INCH_PER_CM = 2.54f; | |
public static float pixelToCms(float pixels, float dpi) { | |
return (pixels / dpi) * INCH_PER_CM; | |
} | |
public static float cmsToPixel(float cms, float dpi) { | |
return cmToInches(cms) * dpi; | |
} | |
public static float cmToInches(float cms) { | |
return cms * CM_PER_INCH; | |
} | |
protected class ContentPane extends JPanel implements Scrollable { | |
private Dimension pageSize; | |
public ContentPane(int pageWidth, int pageHeight) { | |
setLayout(new GridBagLayout()); | |
setBackground(Color.DARK_GRAY); | |
pageSize = new Dimension(pageWidth + 16, pageHeight + 16); | |
} | |
public void addPreview(PagePreviewPane preview) { | |
GridBagConstraints gbc = new GridBagConstraints(); | |
gbc.gridwidth = GridBagConstraints.REMAINDER; | |
gbc.weightx = 1; | |
gbc.fill = gbc.HORIZONTAL; | |
gbc.insets = new Insets(8, 8, 8, 8); | |
super.add(preview, gbc); | |
} | |
@Override | |
public Component add(Component comp) { | |
throw new IllegalArgumentException("Use addPreview"); | |
} | |
@Override | |
public void add(Component comp, Object constraints) { | |
throw new IllegalArgumentException("Use addPreview"); | |
} | |
@Override | |
public Component add(Component comp, int index) { | |
throw new IllegalArgumentException("Use addPreview"); | |
} | |
@Override | |
public Component add(String name, Component comp) { | |
throw new IllegalArgumentException("Use addPreview"); | |
} | |
@Override | |
public void add(Component comp, Object constraints, int index) { | |
throw new IllegalArgumentException("Use addPreview"); | |
} | |
@Override | |
public Dimension getPreferredScrollableViewportSize() { | |
return pageSize; | |
} | |
@Override | |
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { | |
return 32; | |
} | |
@Override | |
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { | |
return 32; | |
} | |
@Override | |
public boolean getScrollableTracksViewportWidth() { | |
return false; | |
} | |
@Override | |
public boolean getScrollableTracksViewportHeight() { | |
return false; | |
} | |
} | |
protected class PagePreviewPane extends JPanel { | |
public PagePreviewPane(BufferedImage img) { | |
setLayout(new BorderLayout()); | |
add(new JLabel(new ImageIcon(img))); | |
} | |
} | |
} | |
public static final String[][] NAMES = new String[][]{ | |
new String[]{"Laura", "Williams"}, | |
new String[]{"Andrea", "Ellis"}, | |
new String[]{"John", "King"}, | |
new String[]{"Kenneth", "Garcia"}, | |
new String[]{"William", "Miller"}, | |
new String[]{"William", "Benitez"}, | |
new String[]{"Michelle", "Hansen"}, | |
new String[]{"Nicholas", "Jones"}, | |
new String[]{"Joshua", "Sanchez"}, | |
new String[]{"Stephen", "Herring"}, | |
new String[]{"Kyle", "Wallace"}, | |
new String[]{"Oscar", "Wong"}, | |
new String[]{"Shane", "Santana"}, | |
new String[]{"Jeffrey", "Weber"}, | |
new String[]{"Laura", "Hale"}, | |
new String[]{"Dr.", "Henry"}, | |
new String[]{"Tammy", "Mathis"}, | |
new String[]{"Jennifer", "Rodriguez"}, | |
new String[]{"Joshua", "Tucker"}, | |
new String[]{"Alejandra", "Wong"}, | |
new String[]{"Barbara", "Flores"}, | |
new String[]{"Kristin", "Sims"}, | |
new String[]{"Stephanie", "Green"}, | |
new String[]{"Travis", "Parks"}, | |
new String[]{"Brian", "Meyers"}, | |
new String[]{"Haley", "Casey"}, | |
new String[]{"Laura", "Wilson"}, | |
new String[]{"Sharon", "Berg"}, | |
new String[]{"Joshua", "Warren"}, | |
new String[]{"William", "Martin"}, | |
new String[]{"David", "Ramos"}, | |
new String[]{"Jessica", "Dennis"}, | |
new String[]{"Joel", "Ferrell"}, | |
new String[]{"Michael", "Johnson"}, | |
new String[]{"Kim", "Watkins"}, | |
new String[]{"Loretta", "Reed"}, | |
new String[]{"Jeffrey", "Williams"}, | |
new String[]{"Jennifer", "Hale"}, | |
new String[]{"Alicia", "Padilla"}, | |
new String[]{"Ian", "Wagner"}, | |
new String[]{"Jasmine", "Wheeler"}, | |
new String[]{"Cynthia", "Aguilar"}, | |
new String[]{"Justin", "Flores"}, | |
new String[]{"Mitchell", "Stephens"}, | |
new String[]{"Kristi", "Rodriguez"}, | |
new String[]{"Renee", "Young"}, | |
new String[]{"Shane", "Simmons"}, | |
new String[]{"Beverly", "Werner"}, | |
new String[]{"Jordan", "Townsend"}, | |
new String[]{"Carrie", "Solomon"}, | |
new String[]{"Jessica", "Martin"}, | |
new String[]{"John", "Pearson"}, | |
new String[]{"Steven", "Miranda"}, | |
new String[]{"Jennifer", "Knight"}, | |
new String[]{"Lindsay", "Martinez"}, | |
new String[]{"Joshua", "Roy"}, | |
new String[]{"Jerry", "Bailey"}, | |
new String[]{"Lauren", "Barr"}, | |
new String[]{"Frank", "Castaneda"}, | |
new String[]{"Gary", "Franklin"}, | |
new String[]{"Robert", "Lewis"}, | |
new String[]{"Peter", "Vasquez"}, | |
new String[]{"Brittany", "Rich"}, | |
new String[]{"Jacob", "White"}, | |
new String[]{"Anna", "Smith"}, | |
new String[]{"Michelle", "Davis"}, | |
new String[]{"Cesar", "Frank"}, | |
new String[]{"Chad", "Walsh"}, | |
new String[]{"Thomas", "Johnson"}, | |
new String[]{"Susan", "Wilkerson"}, | |
new String[]{"Hunter", "Garrett"}, | |
new String[]{"Molly", "Hernandez"}, | |
new String[]{"Gary", "Richmond"}, | |
new String[]{"Megan", "Price"}, | |
new String[]{"Daniel", "Mack"}, | |
new String[]{"Margaret", "Andrade"}, | |
new String[]{"Erika", "White"}, | |
new String[]{"Laura", "Carr"}, | |
new String[]{"Robin", "Schultz"}, | |
new String[]{"Valerie", "King"}, | |
new String[]{"Jacob", "Sherman"}, | |
new String[]{"Monique", "King"}, | |
new String[]{"Laura", "Strickland"}, | |
new String[]{"Jonathan", "Zuniga"}, | |
new String[]{"Danny", "Taylor"}, | |
new String[]{"Darrell", "Reese"}, | |
new String[]{"Juan", "Watkins"}, | |
new String[]{"Valerie", "Cohen"}, | |
new String[]{"David", "Ortiz"}, | |
new String[]{"Catherine", "Hawkins"}, | |
new String[]{"William", "Parker"}, | |
new String[]{"Christine", "Freeman"}, | |
new String[]{"Corey", "Keller"}, | |
new String[]{"James", "Hicks"}, | |
new String[]{"Nicole", "Petty"}, | |
new String[]{"Alexandria", "Aguirre"}, | |
new String[]{"Heather", "Kim"}, | |
new String[]{"Nichole", "Palmer"}, | |
new String[]{"Jonathan", "Moore"}, | |
new String[]{"Cynthia", "Gibbs"} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment