Reputation:
PrinterJob job = PrinterJob.getPrinterJob();
job.setJobName("jPanel2");
job.setPrintable(new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum) {
if (pageNum > 0) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
jPanel2.paint(g2);`enter code here`
return Printable.PAGE_EXISTS;
}
});
I have used the above code to print the jPanel content. But i want to print the data present in jTable. How can i do that?
Upvotes: 4
Views: 191
Reputation: 30
//bill printing
JFrame frame = new JFrame("Bill");
frame.getContentPane().setBackground(Color.WHITE);
frame.getContentPane().setLayout(new FlowLayout());
frame.setPreferredSize(new Dimension(400, 750));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane jp = new JScrollPane();
frame.add(jp);
Container pane = frame.getContentPane();
pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JLabel button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
button = new JLabel("Qty");
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
pane.add(button, c);
button = new JLabel("Item");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 2;
pane.add(button, c);
button = new JLabel("Amount");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 2;
pane.add(button, c);
button = new JLabel("XYZ");
button.setForeground(Color.red);
button.setFont(button.getFont().deriveFont(22.0f));
button.setHorizontalAlignment(SwingConstants.CENTER);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JLabel("Ph.:4466266621");
button.setForeground(Color.red);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 2;
c.gridx = 1;
c.gridy = 1;
pane.add(button, c);
button = new JLabel("Reciept.: " + rn);
button.setForeground(Color.red);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 2;
c.gridx = 2;
c.gridy = 1;
pane.add(button, c);
JSeparator js = new JSeparator();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 3;
pane.add(js, c);
try {
String sql = "SELECT * FROM order_detail";
rs = st.executeQuery(sql);
while (rs.next()) {
j = 0;
button = new JLabel("" + rs.getInt("Quantity"));
c.weightx = 0.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = j++;
c.gridy = i;
pane.add(button, c);
button = new JLabel(rs.getString("Name"));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.gridx = j++;
c.gridy = i;
pane.add(button, c);
button = new JLabel("" + rs.getInt("cost"));
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.gridx = j++;
c.gridy = i;
pane.add(button, c);
i++;
}
js = new JSeparator();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = i++;
pane.add(js, c);
button = new JLabel("Total : " + summedval);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1; //aligned with button 2
c.gridy = i; //third row
c.ipady = 0; //reset to default
c.weighty = 0.5; //request any extra vertical space
c.insets = new Insets(5, 0, 0, 0); //top padding
pane.add(button, c);
frame.pack();
frame.setVisible(true);
} catch (SQLException e) {
e.printStackTrace();
}
PrinterJob job = PrinterJob.getPrinterJob();
job.setJobName("frame");
job.setPrintable(new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum) {
if (pageNum > 0) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
frame.paint(g2);
return Printable.PAGE_EXISTS;
}
});
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
}
}
frame.setVisible(false);
Upvotes: 0
Reputation: 83
public void printJavaComponent() {
PrinterJob job = PrinterJob.getPrinterJob();
job.setJobName("Print Java Component");
job.setPrintable (new Printable() {
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
component_name_to_be_printed.paint(g2d);
return(PAGE_EXISTS);
}
}
});
if (job.printDialog()) {
try {
job.print();
} catch (PrinterException e) {
System.err.println(e.getMessage());
}
}
}
Please try this code i think it helps you.
Upvotes: 1
Reputation: 83
Try this:
PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
set.add(OrientationRequested.LANDSCAPE);
resultFxTable.print(JTable.PrintMode.FIT_WIDTH, header, footer, false, set, false);
Upvotes: 2