Something is wrong with the print command.
-
I'm not able to get the printout. Here is the code.
public class Billing extends Frame implements ActionListener,Printable { JButton printbtn = new JButton("Print"); } public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { return NO\_SUCH\_PAGE; } // User (0,0) is typically outside the // imageable area, so we must translate // by the X and Y values in the PageFormat // to avoid clipping. Graphics2D g2d = (Graphics2D)graphics; g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); // Now we perform our rendering graphics.drawString("Hello world!", 100, 100); // tell the caller that this page is part // of the printed document return PAGE\_EXISTS; // TODO Auto-generated method stub } public Billing ( printbtn.addActionListener(this); JPanel panel = new JPanel(); panel.setLayout(new MigLayout("debug,fillx,filly", "\[\]rel:push\[\]", "\[\]1\[\]")); panel.add(printbtn); frame.add(panel); frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE); frame.pack(); frame.setVisible(true); ) @Override public void actionPerformed(ActionEvent e) { else if (e.getSource().equals(printbtn)) { System.out.println("You have clicked on Print Button!"); PrinterJob pj = PrinterJob.getPrinterJob(); job.setPrintable(this); if (pj.printDialog() == true) { try { pj.print(); } catch (PrinterException exc) { System.out.println(exc); } } System.out.println("Print Job Ends Here!!!!XXXXX!!!!"); } }
-
I'm not able to get the printout. Here is the code.
public class Billing extends Frame implements ActionListener,Printable { JButton printbtn = new JButton("Print"); } public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { return NO\_SUCH\_PAGE; } // User (0,0) is typically outside the // imageable area, so we must translate // by the X and Y values in the PageFormat // to avoid clipping. Graphics2D g2d = (Graphics2D)graphics; g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); // Now we perform our rendering graphics.drawString("Hello world!", 100, 100); // tell the caller that this page is part // of the printed document return PAGE\_EXISTS; // TODO Auto-generated method stub } public Billing ( printbtn.addActionListener(this); JPanel panel = new JPanel(); panel.setLayout(new MigLayout("debug,fillx,filly", "\[\]rel:push\[\]", "\[\]1\[\]")); panel.add(printbtn); frame.add(panel); frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE); frame.pack(); frame.setVisible(true); ) @Override public void actionPerformed(ActionEvent e) { else if (e.getSource().equals(printbtn)) { System.out.println("You have clicked on Print Button!"); PrinterJob pj = PrinterJob.getPrinterJob(); job.setPrintable(this); if (pj.printDialog() == true) { try { pj.print(); } catch (PrinterException exc) { System.out.println(exc); } } System.out.println("Print Job Ends Here!!!!XXXXX!!!!"); } }
Please get your code to a compilable form - everyone trying to read this has to guess what you actually mean several times to get that right. Regards, ab
-
Please get your code to a compilable form - everyone trying to read this has to guess what you actually mean several times to get that right. Regards, ab
Sorry my bad with Copy/Paste Here is the code once again.
public class Billing extends Frame implements ActionListener,Printable { JButton printbtn = new JButton("Print"); JFrame frame = new JFrame(); PrinterJob job = PrinterJob.getPrinterJob(); public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { return NO\_SUCH\_PAGE; } // User (0,0) is typically outside the // imageable area, so we must translate // by the X and Y values in the PageFormat // to avoid clipping. Graphics2D g2d = (Graphics2D)graphics; /\*Rectangle2D.Double rectangle = new Rectangle2D.Double (); rectangle.setRect (pageFormat.getImageableX () + 72, pageFormat.getImageableY () + 72, 72, 72); g2d.draw (rectangle);\*/ g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); // Now we perform our rendering graphics.drawString("Hello world!", 100, 100); // tell the caller that this page is part // of the printed document return PAGE\_EXISTS; // TODO Auto-generated method stub } public Billing() { JPanel panel = new JPanel(); panel.setLayout(new MigLayout("debug,fillx,filly", "\[\]rel:push\[\]", "\[\]1\[\]")); panel.add(printbtn); frame.add(panel); printbtn.addActionListener(this); Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); int x=(int)((dimension.getWidth() - 760)/2); int y=(int)((dimension.getHeight() - 550)/2); frame.setLocation(x, y); frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE); frame.pack(); frame.setVisible(true); public static void main(String\[\] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } new Billing(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource().equals(printbtn)) { System.out.println("You have clicked on Print Button!"); PrinterJob pj = PrinterJob.getPrinterJob(); job.setPrintable(this); if (pj.printDialog() == true) { try { pj.print(); } catch (PrinterException exc) { exc.printStackTrace(); } } System.out.println("Print Job Ends Here!!!!XXXXX!!!!"); } } } }
-
Sorry my bad with Copy/Paste Here is the code once again.
public class Billing extends Frame implements ActionListener,Printable { JButton printbtn = new JButton("Print"); JFrame frame = new JFrame(); PrinterJob job = PrinterJob.getPrinterJob(); public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { return NO\_SUCH\_PAGE; } // User (0,0) is typically outside the // imageable area, so we must translate // by the X and Y values in the PageFormat // to avoid clipping. Graphics2D g2d = (Graphics2D)graphics; /\*Rectangle2D.Double rectangle = new Rectangle2D.Double (); rectangle.setRect (pageFormat.getImageableX () + 72, pageFormat.getImageableY () + 72, 72, 72); g2d.draw (rectangle);\*/ g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); // Now we perform our rendering graphics.drawString("Hello world!", 100, 100); // tell the caller that this page is part // of the printed document return PAGE\_EXISTS; // TODO Auto-generated method stub } public Billing() { JPanel panel = new JPanel(); panel.setLayout(new MigLayout("debug,fillx,filly", "\[\]rel:push\[\]", "\[\]1\[\]")); panel.add(printbtn); frame.add(panel); printbtn.addActionListener(this); Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); int x=(int)((dimension.getWidth() - 760)/2); int y=(int)((dimension.getHeight() - 550)/2); frame.setLocation(x, y); frame.setDefaultCloseOperation(JFrame.DISPOSE\_ON\_CLOSE); frame.pack(); frame.setVisible(true); public static void main(String\[\] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } new Billing(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource().equals(printbtn)) { System.out.println("You have clicked on Print Button!"); PrinterJob pj = PrinterJob.getPrinterJob(); job.setPrintable(this); if (pj.printDialog() == true) { try { pj.print(); } catch (PrinterException exc) { exc.printStackTrace(); } } System.out.println("Print Job Ends Here!!!!XXXXX!!!!"); } } } }
-
So two copies of your code but with no detail about your problem. Please explain exactly what you are trying to do, what results you expect, and what results you see, including the full text of any error messages.
Veni, vidi, abiit domum
I'm able to print Hello world with the help of my printer machine. But with that code I don't get any error messages,but I'm able to print those two lines in the Console 1.You have clicked on Print Button! 2.Print Job Ends Here!!!!XXXXX!!!! But not Hello world from this code
graphics.drawString("Hello world!", 100, 100);
What is wrong with this code?
-
I'm able to print Hello world with the help of my printer machine. But with that code I don't get any error messages,but I'm able to print those two lines in the Console 1.You have clicked on Print Button! 2.Print Job Ends Here!!!!XXXXX!!!! But not Hello world from this code
graphics.drawString("Hello world!", 100, 100);
What is wrong with this code?
-
I don't see any implementation of the
Printable
Interface'sprint
method[^] in your code.Veni, vidi, abiit domum
I have got the solution I have created two Printer Job 1.
PrinterJob job = PrinterJob.getPrinterJob();
PrinterJob pj = PrinterJob.getPrinterJob();
And If I did this in my code
job.setPrintable(this);
I should not be doing this
if (pj.printDialog() == true)
pj.print();Instead of "pj" I have to use "job". that was the problem I was not using the right job hehe.