0

I'm trying to print a billing receipt in NetBeans.

Billing receipt is getting table data from another table data, Everything is working fine.

Problem is that when I add rows more then the length of JTable it makes a scroll bar and data under scrollbar bar is not printing.

I want to print my complete JTable whether that is in scrollbar or not. Software Should print the complete bill including components of JFrame and Complete JTable. Here is my Code:

package project;

import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class reciept extends javax.swing.JPanel implements Printable{
static DateFormat date=new SimpleDateFormat("dd-M-yyyy");

JFrame f=new JFrame();

static SimpleDateFormat time=new SimpleDateFormat("h:m:s a");

static Date d=new Date(System.currentTimeMillis());  
Connection conn;

/**
 * Creates new form reciept
 */
public reciept() {
  initComponents();
  conn=DB.con();       
  cal();
  jTable1.setBackground(Color.white);
  }

 @Override
public int print(Graphics g, PageFormat pf, int i) throws PrinterException {
    if (i > 0) {
        return NO_SUCH_PAGE;
    }
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    reciept.this.printAll(g);
    return Printable.PAGE_EXISTS;
}

public void cal(){
    reciept.jTable1.getTableHeader().setFont(new Font("Dialog",Font.PLAIN,7));
    jTable1.setBackground(Color.decode("#FFFFFF"));

 String s=date.format(d);
    String ta=time.format(d);

     t.setText(ta);

    da.setText(s);
    jLabel12.setText(sale.ca);
    String cas=String.valueOf(sale.bill);
    jLabel14.setText(cas);

        tb.setText(sale.b);

     double sum=0;

     for(int i=0; i<jTable1.getRowCount(); i++)
     {

           String d=jTable1.getValueAt(i, 3).toString();
     double d1=Double.parseDouble(d);
    sum+=d1;
     }

     String q1=String.valueOf(sum);

     tq.setText(q1);
     //-----------------------------------------------------------------------------------------------------------------------
try {
    Statement st=conn.createStatement();

    String sql="SELECT billnumber From bill";
    ResultSet rs=st.executeQuery(sql);
    if(rs.next())
    {
    int bill=rs.getInt("billnumber");

    String bnum=String.valueOf(bill);
    bn.setText(bnum);
    bill++;
    st.executeUpdate("UPDATE bill Set billnumber='"+bill+"'");
    }
} catch (Exception ex) {
    Logger.getLogger(reciept.class.getName()).log(Level.SEVERE, null, ex);
}
}

/**
 * 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jSeparator1 = new javax.swing.JSeparator();
    bn = new javax.swing.JLabel();
    da = new javax.swing.JLabel();
    t = new javax.swing.JLabel();
    jLabel9 = new javax.swing.JLabel();
    tq = new javax.swing.JLabel();
    jLabel10 = new javax.swing.JLabel();
    tb = new javax.swing.JLabel();
    jSeparator2 = new javax.swing.JSeparator();
    jLabel14 = new javax.swing.JLabel();
    jLabel13 = new javax.swing.JLabel();
    jLabel12 = new javax.swing.JLabel();
    jLabel11 = new javax.swing.JLabel();
    jSeparator6 = new javax.swing.JSeparator();
    jLabel15 = new javax.swing.JLabel();
    jLabel16 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    Object[] col={ "S#","Dis","Price","Qty","Disc","Amt"};

    DefaultTableModel tm=new DefaultTableModel(sale.tabledata,col);
    jTable1 = new javax.swing.JTable();
    jLabel8 = new javax.swing.JLabel();

    jPanel1.setBackground(new java.awt.Color(255, 255, 255));
    jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/project/logo.png"))); // NOI18N
    jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(56, 0, 74, 45));

    jLabel2.setFont(new java.awt.Font("Trajan Pro", 1, 21)); // NOI18N
    jLabel2.setText("Tayyab Fabrics");
    jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(22, 51, -1, -1));

    jLabel3.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel3.setText("Shop No. G-1, Rabi Center , Hariyanwala Chowk, D-Ground");
    jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 79, 400, -1));

    jLabel4.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel4.setText("Ph #  041-8546800");
    jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(73, 108, 105, -1));

    jLabel5.setFont(new java.awt.Font("Aharoni", 0, 8)); // NOI18N
    jLabel5.setText("Sales Receipt");
    jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(64, 146, -1, 21));

    jSeparator1.setForeground(new java.awt.Color(0, 0, 0));
    jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 124, 223, -1));

    bn.setFont(new java.awt.Font("Agency FB", 0, 8)); // NOI18N
    bn.setText("Number # !");
    jPanel1.add(bn, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 149, 36, 14));

    da.setFont(new java.awt.Font("Agency FB", 0, 8)); // NOI18N
    da.setText("Date");
    jPanel1.add(da, new org.netbeans.lib.awtextra.AbsoluteConstraints(79, 129, 83, 16));

    t.setFont(new java.awt.Font("Agency FB", 0, 8)); // NOI18N
    t.setText("Time");
    jPanel1.add(t, new org.netbeans.lib.awtextra.AbsoluteConstraints(147, 146, 85, 21));

    jLabel9.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel9.setText("Total Qty");
    jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 380, -1, -1));

    tq.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    tq.setText("jLabel10");
    jPanel1.add(tq, new org.netbeans.lib.awtextra.AbsoluteConstraints(51, 380, 40, 10));

    jLabel10.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel10.setText("Total");
    jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(104, 381, 31, -1));

    tb.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    tb.setText("jLabel11");
    jPanel1.add(tb, new org.netbeans.lib.awtextra.AbsoluteConstraints(151, 383, 50, 7));

    jSeparator2.setForeground(new java.awt.Color(0, 0, 0));
    jPanel1.add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 397, 224, -1));

    jLabel14.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel14.setText("jLabel14");
    jPanel1.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(152, 405, 70, 17));

    jLabel13.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel13.setText("Cash return");
    jPanel1.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(101, 405, -1, 16));

    jLabel12.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel12.setText("jLabel12");
    jPanel1.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(55, 405, 40, 16));

    jLabel11.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel11.setText("Cash Recieved");
    jPanel1.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 410, -1, -1));

    jSeparator6.setForeground(new java.awt.Color(0, 0, 0));
    jPanel1.add(jSeparator6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 479, 237, -1));

    jLabel15.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel15.setText("Sold product can be exchanged within 8 days");
    jPanel1.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 428, 209, 14));

    jLabel16.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel16.setText("Thank you for Shoping here");
    jPanel1.add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(37, 448, 160, -1));

    jLabel6.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel6.setText("Software Contact:");
    jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(22, 464, 65, 9));

    jLabel7.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel7.setText("+92 306 909 6975");
    jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(97, 464, 90, 9));

    jTable1.setFont(new java.awt.Font("Dialog", 0, 6));
    jTable1.setModel(tm);
    jTable1.setEnabled(false);
    jTable1.setFocusable(false);
    jTable1.setRowHeight(14);
    jScrollPane1.setViewportView(jTable1);

    jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 173, 192, 40));

    jLabel8.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
    jLabel8.setText("Faisalalabad");
    jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(84, 92, 58, -1));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 0, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 487, javax.swing.GroupLayout.PREFERRED_SIZE)
    );
}// </editor-fold>                        

// Variables declaration - do not modify                     
private javax.swing.JLabel bn;
private javax.swing.JLabel da;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator6;
public static javax.swing.JTable jTable1;
private javax.swing.JLabel t;
private javax.swing.JLabel tb;
private javax.swing.JLabel tq;
// End of variables declaration                   

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            final reciept pt = new reciept();
            f.add(pt, BorderLayout.CENTER);
          JButton b = new JButton(new AbstractAction("Print") {

                @Override
                public void actionPerformed(ActionEvent e) {
                  PrinterJob pj = PrinterJob.getPrinterJob();
                    Paper paper = new Paper();
                     PageFormat pf = pj.defaultPage();
                     pf.setPaper(paper);
    pf.setOrientation(PageFormat.PORTRAIT);
                    pj.setPrintable(pt, pf);

                        try {
                            pj.print();
                        } catch (PrinterException pe) {
                            pe.printStackTrace(System.err);
                        }

           }
            });
              f.getRootPane().setDefaultButton(b);
     b.requestFocus();
            JPanel p = new JPanel();
         p.add(b);
         p.setBackground(Color.BLACK);
            f.add(p, BorderLayout.NORTH);

            f.pack();

            f.setVisible(true);

        }
    });
}
}

How to print complete JFrame and all rows of JTable?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Numan Pathan
  • 149
  • 7
  • Do you want to take a Screen shot or how dou do define "print out"? How do you think about using a reporting system like JasperReport or exporting your table data to csv or something? – Maia Sep 07 '17 at 12:45
  • In [this answer](https://stackoverflow.com/a/42538618/2180785) I used `iText`, not `PrinterJob` as I think it provides more flexibility. You could take one of the various options I gave there and try to implement it using `PrinterJob` or switch to `iText`. However you could take at [`JTable#printable()`](https://docs.oracle.com/javase/tutorial/uiswing/misc/printtable.html) (as discussed in the linked answer by @MadProgrammer) however I've never worked with it, so you may do some research. Also, for better help sooner post a proper [mcve] – Frakcool Sep 07 '17 at 14:25
  • The "answer" would be to make use of the `JFrame`'s `printAll` method, you can have a look at [this for an example](https://stackoverflow.com/questions/16728397/print-the-whole-program-layout/16728708#16728708) - but I doubt very much if this is what you really want to do, as the `JTable` may contain more rows then the `JFrame` is showing (and if it's contained in a `JScrollPane`, you have more issues), instead, you should take advantage of `JTable`s print support, [for example](https://stackoverflow.com/questions/14544013/how-to-print-a-jtable-object-in-the-java-application/14544898#14544898) – MadProgrammer Sep 07 '17 at 22:01
  • For more complex reporting, I'd recommend using something like Jasper Reports, have a look at the two answers in [this question](https://stackoverflow.com/questions/34086377/printing-a-jpanel-with-scrollable-jtable-on-it/34114959#34114959) for a comparison – MadProgrammer Sep 07 '17 at 22:03
  • 1) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Sep 07 '17 at 22:58
  • Maia i dont knw how to take out print i just want to my complete frame. – Numan Pathan Sep 08 '17 at 05:53
  • let me explain about... i have a jFrame having some components with free design in netbeans jTable is middle of all components and when i add more rows there. a scrollbar apears and when i runn programe and click at print button it just take screenshot of the frame and print it out. everything is going well as should . but problem is that rows that goes to scrollbar in Jtable are hidden when system take screenshot. this is what i want to resolve... i want that to print Complete JTable along with JFrame – Numan Pathan Sep 08 '17 at 05:56
  • Do you understand? – Numan Pathan Sep 08 '17 at 05:57
  • my Jtable is getting data from another table by TableModel everything is fine just problem is programe takes screenshot and print that screenshot. this is my problem – Numan Pathan Sep 08 '17 at 05:58
  • Tip: add @AndrewThompson (or whoever you're replying to, the @ is important) so they get notified. What you're trying to achieve is kind of "weird" since the JTable is having a JScrollBar so, your GUI in the screenshot might get truncated or painted over by the table if you manage to do it. – Frakcool Sep 08 '17 at 16:36
  • 1
    Again, rethink your PDF idea, as you can get either the GUI with truncated data or the data using JasperReports (as suggested by @MadProgrammer) / the method I posted which prints the table data along with its properties (border color / font color / etc) – Frakcool Sep 08 '17 at 16:36
  • Okey I have solved it with absolute layout in which components are positioned by margin of jtable and jtable is set to height I mean there is no scroll bar – Numan Pathan Sep 08 '17 at 18:17
  • It would be a good idea to post your answer – Frakcool Sep 08 '17 at 20:15
  • @Frakcool Not that answer – MadProgrammer Sep 08 '17 at 22:44
  • @MadProgrammer Because of absolute layout? Last OP's comment was a bit unclear to me (maybe due to me not being a native English speaker or due to him not writing it clearly), so I wanted to understand with code what he was trying to do – Frakcool Sep 08 '17 at 22:49

0 Answers0