Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Something is wrong with the print command.

Something is wrong with the print command.

Scheduled Pinned Locked Moved Java
graphicsdebuggingcareer
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chdboy
    wrote on last edited by
    #1

    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!!!!");
    }
    }
    
    A 1 Reply Last reply
    0
    • C chdboy

      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!!!!");
      }
      }
      
      A Offline
      A Offline
      angrybobcat
      wrote on last edited by
      #2

      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

      C 1 Reply Last reply
      0
      • A angrybobcat

        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

        C Offline
        C Offline
        chdboy
        wrote on last edited by
        #3

        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!!!!");
        }
        }
        }
        }
        
        L 1 Reply Last reply
        0
        • C chdboy

          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!!!!");
          }
          }
          }
          }
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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

          C 1 Reply Last reply
          0
          • L Lost User

            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

            C Offline
            C Offline
            chdboy
            wrote on last edited by
            #5

            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?

            L 1 Reply Last reply
            0
            • C chdboy

              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?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I don't see any implementation of the Printable Interface's print method[^] in your code.

              Veni, vidi, abiit domum

              C 1 Reply Last reply
              0
              • L Lost User

                I don't see any implementation of the Printable Interface's print method[^] in your code.

                Veni, vidi, abiit domum

                C Offline
                C Offline
                chdboy
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups