I am making the application for printing the content of JFrame .It works properly but my problem is that I have to call that application on “Print” JButton which is on different form such as Print JButton is on Form_1.java and Printing content is on Form_2.java…
I am Using ActionListener on Print Button but it not Work..
pls help me my project is on deadline pls…
I posted My code Here:
public class PrintUI implements Printable, ActionListener {
JFrame frameToPrint;
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY() - 55);
frameToPrint.setVisible(true);
frameToPrint.print(g);
return PAGE_EXISTS;
}
public void actionPerformed(ActionEvent e) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
}
}
}
public PrintUI(JFrame f) {
frameToPrint = f;
}
public static void main(String args[]) {
final JButton printButton = new JButton("Print");
UIManager.put("swing.boldMetal", Boolean.TRUE);
JFrame f = new JFrame("GatePass");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Font f1 = new Font("Times New Roman", Font.BOLD, 12);
printButton.setVisible(true);
JLabel label1 = new JLabel("Vision Techno Solutions", JLabel.CENTER);
label1.setBounds(30, 30, 200, 40);
JLabel label2 = new JLabel("Batch No:");
label2.setBounds(10, 70, 80, 20);
JLabel label3 = new JLabel("Date and Time:");
label3.setBounds(10, 100, 120, 20);
JLabel label4 = new JLabel("Visitor's Name:");
label4.setBounds(10, 130, 120, 20);
JLabel label5 = new JLabel("Concern Person:");
label5.setBounds(10, 160, 120, 20);
JLabel label6 = new JLabel("Purpose of Visit:");
label6.setBounds(10, 190, 120, 20);
JLabel label7 = new JLabel("Manager's Sign");
label7.setBounds(140, 230, 120, 20);
JLabel label8 = new JLabel("Visitor's Sign");
label8.setBounds(10, 230, 120, 20);
printButton.addActionListener(new PrintUI(f));
f.setSize(300, 350);
f.setLocationRelativeTo(null);
f.add(printButton, BorderLayout.SOUTH);
f.setResizable(false);
f.setVisible(true);
printButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
printButton.setVisible(false);
}
});
}
}
Not sure what exactly you are trying to do here.
You said that you are trying to print another Frame but
indicates you are adding
PrintUIclass to print the current frame.If you want to print another frame, first create another frame in your code, make it visible, and then pass the new frame to
PrintUI(), that should work.