Search code examples
javaswingprintingjtextpane

Java Swing Invoice String.format returning only for () {} value to JTextPane?


Printer job only printing one row from JTable. Only getting one output by inputting one or more rows:

public void invoice_job()   {
    try {

        int rows=JTable.getRowCount();

        for (int row = 0; row<rows; row++) {    

            Names = (String)JTable.getValueAt(row, 0);
            Quanti = (String)JTable.getValueAt(row, 1);
            Prices = (Double)JTable.getValueAt(row, 2);

            String head = String.format("%30s\n\n", "   Invoice Reciept   ");
            String s = String.format("%-15s %5s %10s\n", "Item",        "Qty",       "Price");
            String s1 = String.format("%-15s %5s %10s\n","---------","--------"," ----------");                             
            String line = String.format("%-15s %5s %10.2f\n",Names,Quanti,Prices);  
            String lin2 = String.format("%-15s\n","-------------------------------------");
            String Total = String.format("%37s\n","Total");
            String total = String.format("%37s\n\n",lbl.getText());             
            String footer = String.format("%-15s\n", "Meridon sweets and bakers\nSince 1987\nPoint of sale system\nPowered by msofttech\nwww.msofttech.net");
            String output = head + s + s1 + line + lin2 + Total + total + footer;               
            textPane.setText(output);
            textPane.setText(output);
            textPane.setText(output);
            textPane.setText(output);
            JOptionPane.showMessageDialog(null, output);

        }       

        PrinterJob printerJob = PrinterJob.getPrinterJob();
        PageFormat pageFormat = printerJob.defaultPage();
        Paper paper = new Paper();
        paper.setSize(180.0, (double) (paper.getHeight() + rows * 10.0));
        paper.setImageableArea(rows, rows, paper.getWidth() - rows * 2,   
        paper.getHeight() - rows * 2);
        pageFormat.setPaper(paper);
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        printerJob.setPrintable(textPane.getPrintable(null, null), pageFormat);
        printerJob.print();

    } 
    catch (Exception e) {
        e.printStackTrace();
    }

Input:

image

Input are 2 items.

Output:

image

Output is only one item.


Solution

  • Try this code below :

       public void invoice_job()   
       {
    
    
    
        try 
        {
    
          int rows=JTable.getRowCount();
          String head = String.format("%30s\n\n", "   Invoice Reciept   ");
          String s = String.format("%-15s %5s %10s\n", "Item",        "Qty",        "Price");
          String s1 = String.format("%-15s %5s %10s\n","---------","--------"," ----------");    
          String output = head + s + s1;
          for(int row = 0; row<rows; row++)
    
          {   
    
             String Names = (String)JTable.getValueAt(row, 0);
             String Quanti = (String)JTable.getValueAt(row, 1);
             String Prices = (Double)JTable.getValueAt(row, 2);
    
             String line = String.format("%-15s %5s %10.2f\n",Names,Quanti,Prices);  
             output += line;
             String lin2 = String.format("%-15s\n","-------------------------------------");
             output += lin2;
    
    
    
           }
    
           String Total = String.format("%37s\n","Total");
           String total = String.format("%37s\n\n",lbl.getText()); 
           String footer = String.format("%-15s\n", "Meridon sweets and bakers\nSince 1987\nPoint of sale system\nPowered by msofttech\nwww.msofttech.net");
           output += Total + total + footer;
           textPane.setText(output);
    
           JOptionPane.showMessageDialog(null, output);
    
    
           PrinterJob printerJob = PrinterJob.getPrinterJob();
           PageFormat pageFormat = printerJob.defaultPage();
           Paper paper = new Paper();
           paper.setSize(180.0, (double) (paper.getHeight() + rows * 10.0));
           paper.setImageableArea(rows, rows, paper.getWidth() - rows * 2, paper.getHeight() - rows * 2);
           pageFormat.setPaper(paper);
           pageFormat.setOrientation(PageFormat.PORTRAIT);
           printerJob.setPrintable(textPane.getPrintable(null, null), pageFormat);
           printerJob.print();
    
        } 
        catch ( Exception e ) 
        {
    
            e.printStackTrace();
        }
    }
    

    You need to add one line for each row to the text pane.