Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6848197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:50:19+00:00 2026-05-27T00:50:19+00:00

The problem with the code below is that it doesn’t print of bk (Java

  • 0

The problem with the code below is that it doesn’t print of bk (Java Book) and only prints off ‘g’ which is the user interface window.

Can someone explain how to print off the Java Book?

public class printInvoice extends javax.swing.JFrame implements Printable {

    JFrame frameToPrint;


        /** Creates new form SimplePrint */
        public void SimplePrint(JFrame f) {
            frameToPrint = f;

        }

        /** Creates new form NewJFrame */
        public printInvoice() {
            initComponents();
            this.setVisible(true);

            print2();

        }

        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")


        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        }                                        

        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                    new printInvoice().setVisible(true);
                }
            });
        }

        public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
            if (pageIndex > 0) {
                return (NO_SUCH_PAGE);
            } else {
                Graphics2D g2d = (Graphics2D) g;
                g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                // Turn off double buffering
                //componentToBePrinted.paint(g2d);
                //frameToPrint.print(g);
                    paint(g);
                  //  this.print(g);

                 //  this.print(g);

                // Turn double buffering back on
                return (PAGE_EXISTS);
            }
        }


        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JList jList1;
        private javax.swing.JRadioButton jRadioButton1;
        private javax.swing.JScrollPane jScrollPane1;
        // End of variables declaration                   
        int userCountAmount;
        dataBase data = new dataBase();
        Book bk = new Book();
        PrinterJob PJ = PrinterJob.getPrinterJob();


        public void print2() {
            userCountAmount = data.getAmountOfUsers();

            PageFormat portrait = PJ.defaultPage();

            portrait.setOrientation(PageFormat.PORTRAIT);


                jLabel1.setText("Print number: "+0);
                bk.append((Printable) this, PJ.defaultPage(),1);
                jLabel1.setText("Print number: "+1);
                bk.append((Printable) this, PJ.defaultPage(),2);
                jLabel1.setText("Print number: "+2);
                bk.append((Printable) this, PJ.defaultPage(),3);
    System.out.println(bk.toString());

            //PageFormat PF   =   PJ.pageDialog(PJ.defaultPage());
            PJ.setPageable(bk);

         //    PJ.setPrintable((Printable) this);
            boolean doPrint = PJ.printDialog();
            //JOptionPane.showMessageDialog(null, doPrint);
            if (doPrint) {
                try {

                        PJ.print();

                } catch (PrinterException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }
            }
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:50:19+00:00Added an answer on May 27, 2026 at 12:50 am

    Printer job is using print(Graphics g, PageFormat pageFormat, int pageIndex) when the book is printed. Your print method calls paint(g), and that is method of your UI frame.

    You shoud use g of the print method for drawing wanted images to pages, if you don’t want to print images of UI controls.

    EDIT: example, that draw text to middle of the page (not tested):

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
        Graphics2D g2d = (Graphics2D) g;
    
        double x1 = pf.getImageableX();
        double y1 = pf.getImageableY();
        double x2 = pf.getImageableWidth();
        double y2 = pf.getImageableHeight();    
    
        g2d.drawString( "Page #" + pageIndex, (int)((x2-x1)/2+x1), (int)((y2-y1)/2+y1) );
    
        return (PAGE_EXISTS);
    }
    

    btw:

    In your code, the following place need to be changed if you want print more pages than one:

        if (pageIndex > 0) {
            return (NO_SUCH_PAGE);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please find my code below. My problem is that I the inner jQuery.get() doesn't
The code below is (hopefully) a minimized testcase of my original problem. I'm trying
The code below is working, but with a problem I do not understand. When
I am having a very specific problem in JQuery The code below is used
Having a problem getting a TreeView control to display node images. The code below
I am having a problem using the signedCMS.decode routine. See the code below. The
I have some problem with my cfml website. I have used the below code
I have the below code that links and runs fine in 32bit mode -
I have a problem understanding what's causes the compilation error in the code below:
Edit: This code is fine. I found a logic bug somewhere that doesn't exist

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.