bonzermahesh 0 Newbie Poster

Hi all,
I need a code which allows me printing of JPanel.
VIMP is the JPanel contains large number of Other components such as Jcheckbox, JLabel, JButton on top & Bottom also, JTabel & JTextarea is inside JScrollpane.

Something like this.

JLabel
JLabel
JLabel   JCheckBox  JCheckBox JCheckBox  JLabel


JTextPane


JTabel in JScrollpane Containing dynamic data coming from DB along with JButton rendering.


JTextArea along with Jscrollpane having dynamic data



JLabel                                                   JLabel


JButton

I hope this will give u clear idea, about what I want.

Please provide me solution so that I can print all it's content on A4 paper.

for your reference I am sending the code

import java.awt.*;
import java.awt.print.*;


public class PrintActivityReport  extends javax.swing.JDialog implements Printable
{
// Variables declaration - do not modify
private javax.swing.JCheckBox followUpRepCheck;
private javax.swing.JCheckBox incidenceRepCheck;
private javax.swing.JCheckBox informationCheck;
private javax.swing.JButton printButton;
private javax.swing.JLabel header;
private javax.swing.JLabel subHeader;
private javax.swing.JLabel locationTab;
private javax.swing.JLabel forPeriod;


private javax.swing.JLabel offSign;
private javax.swing.JLabel traineeSign;


private javax.swing.JPanel ActivityReport;
private javax.swing.JScrollPane incidentTextAreaScrollPane;
private javax.swing.JTextArea incidentTextArea;
private javax.swing.JTextPane clientTextPane;
// End of variables declaration


// Creates object For table
//  PrintViewActivity printContentPane;


/** Creates new form PrintActivityReport */
public PrintActivityReport(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}


// initComponents method
private void initComponents() {
ActivityReport = new javax.swing.JPanel();
header = new javax.swing.JLabel();
subHeader = new javax.swing.JLabel();
locationTab = new javax.swing.JLabel();
forPeriod = new javax.swing.JLabel();
offSign = new javax.swing.JLabel();
traineeSign = new javax.swing.JLabel();
printButton = new javax.swing.JButton();
incidenceRepCheck = new javax.swing.JCheckBox();
informationCheck = new javax.swing.JCheckBox();
followUpRepCheck = new javax.swing.JCheckBox();
clientTextPane = new javax.swing.JTextPane();
incidentTextAreaScrollPane = new javax.swing.JScrollPane();
incidentTextArea = new javax.swing.JTextArea();


setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
ActivityReport.setLayout(null);
ActivityReport.setBackground(new java.awt.Color(255, 255, 255));


setTitle("Activity Report");
setSize(700,720);
java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
java.awt.Dimension dimension = toolkit.getScreenSize();
setLocation(
dimension.width  / 2 - 700 / 2,
dimension.height / 2 - 720 / 2
);


header.setFont(new java.awt.Font("Arial", 1, 14));
header.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
header.setText("CrimeTek Security Services");
ActivityReport.add(header);
header.setBounds(230, 10, 230, 17);


subHeader.setBackground(new java.awt.Color(255, 255, 255));
subHeader.setFont(new java.awt.Font("Arial", 1, 12));
subHeader.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
subHeader.setText("Daily Activity Report");
ActivityReport.add(subHeader);
subHeader.setBounds(250, 30, 160, 20);


locationTab.setFont(new java.awt.Font("Arial", 1, 12));
locationTab.setText("Location : G-1");
ActivityReport.add(locationTab);
locationTab.setBounds(20, 50, 80, 20);


forPeriod.setFont(new java.awt.Font("Arial", 1, 12));
forPeriod.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
forPeriod.setText("From : 2005-11-08      To : 2005-11-21");
forPeriod.setAutoscrolls(true);
ActivityReport.add(forPeriod);
forPeriod.setBounds(430, 50, 220, 15);


offSign.setBackground(new java.awt.Color(255, 255, 255));
offSign.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
offSign.setText("Officer Signature :");
ActivityReport.add(offSign);
offSign.setBounds(10, 610, 150, 14);


traineeSign.setBackground(new java.awt.Color(255, 255, 255));
traineeSign.setText("2nd Officer or Trainee Signature :");
ActivityReport.add(traineeSign);
traineeSign.setBounds(384, 610, 200, 30);


printButton.setBackground(new java.awt.Color(255, 255, 255));
printButton.setText("Print");
ActivityReport.add(printButton);
printButton.setBounds(270, 620, 100, 20);


printButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
printActionPerformed(evt);
}
});


incidenceRepCheck.setBackground(new java.awt.Color(255, 255, 255));
incidenceRepCheck.setFont(new java.awt.Font("Arial", 1, 12));
incidenceRepCheck.setText("INCIDENT REPORT");
ActivityReport.add(incidenceRepCheck);
incidenceRepCheck.setBounds(130, 70, 130, 20);


informationCheck.setBackground(new java.awt.Color(255, 255, 255));
informationCheck.setFont(new java.awt.Font("Arial", 1, 12));
informationCheck.setText(" INFORMATION");
ActivityReport.add(informationCheck);
informationCheck.setBounds(260, 70, 110, 20);


followUpRepCheck.setBackground(new java.awt.Color(255, 255, 255));
followUpRepCheck.setFont(new java.awt.Font("Arial", 1, 12));
followUpRepCheck.setText("FOLLOW UP REPORT");
ActivityReport.add(followUpRepCheck);
followUpRepCheck.setBounds(370, 70, 145, 20);


clientTextPane.setText("Client Name : Henry Tison");
ActivityReport.add(clientTextPane);
clientTextPane.setBounds(80, 100, 470, 20);


// incidentTextArea contains some dynamic data comes from DB
incidentTextAreaScrollPane.setViewportView(incidentTextArea);


ActivityReport.add(incidentTextAreaScrollPane);
incidentTextAreaScrollPane.setBounds(20, 480, 620, 110);


//Initialize PrintViewActivity class to set AbstractTableModel & dynamic data display
//Create and set up the content pane.
/*      printContentPane = new PrintViewActivity();
printContentPane.setOpaque(true); //content panes must be opaque
printActivityReportPanel.add(printContentPane);
printContentPane.setBounds(20, 130, 620, 230);
*/
getContentPane().add(ActivityReport, java.awt.BorderLayout.CENTER);


}



/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PrintActivityReport(new javax.swing.JFrame(), true).setVisible(true);
}
});
}


private void printActionPerformed(java.awt.event.ActionEvent event)
{
printButton.setVisible(false);
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(PrintActivityReport.this);
pj.printDialog();


try{
pj.print();
}
catch (Exception PrintException) {}


printButton.setVisible(true);


this.setVisible(false);
this.dispose();
}



public int print(Graphics g, PageFormat pageFormat,int pageIndex) throws PrinterException
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
int fontHeight=g2.getFontMetrics().getHeight();
int fontDesent=g2.getFontMetrics().getDescent();


//leave room for page number
double pageHeight = pageFormat.getImageableHeight()-fontHeight;
double pageWidth = pageFormat.getImageableWidth();


return Printable.PAGE_EXISTS;
}


}

Thanks & Regards,
Bonzee

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.