Clicky

I need to print text from my JFrame to a dot matrix printer. I want to print it directly without asking confirmation dialog.
I want to print on the paper whose width is 3 inch and height may vary because I am going to use it for super market software where item list may content 2 items or 500 items. It depends on items.
Port of printer may be LPT 1, serial, Usb, etc anything. I want my code to be use for any printer.
Also I want to know that for this purpose I should set the size of page through code or it will automatically? Actually I prefer automatic. So please suggest me solution on that way.

Please help

asked 08/09/2011 03:18

Aurii's gravatar image

Aurii ♦♦


6 Answers:


This is an article about using Java Printing API; it is rather old, but still a good place to
start for Java Printing API:

http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-print.html?page=1
link

answered

for_yan's gravatar image

for_yan

You're probably better off trying to print headlessly:

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/

(search on print)
link

answered 2011-08-09 at 23:29:25

CEHJ's gravatar image

CEHJ

Thank you for reply. I tried these things but its not worked for me.  Please suggest me any solution.
link

answered 2011-08-10 at 02:34:28

Aurii's gravatar image

Aurii

Please post the code you used and show the results, plus any error messages in full
link

answered 2011-08-12 at 23:08:23

CEHJ's gravatar image

CEHJ

Here is my code,

package tushar;

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

public class PrintUtilities implements Printable {
  private Component componentToBePrinted;

  public static void printComponent(Component c) {
    new PrintUtilities(c).print();
  }

  public PrintUtilities(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
  }

  public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    if (printJob.printDialog())
      try {
        printJob.print();
      } catch(PrinterException pe) {
        System.out.println("Error printing: " + pe);
      }
  }

  public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
      return(NO_SUCH_PAGE);
    } else {
      Graphics2D g2d = (Graphics2D)g;
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
      disableDoubleBuffering(componentToBePrinted);
      componentToBePrinted.paint(g2d);
      enableDoubleBuffering(componentToBePrinted);
      return(PAGE_EXISTS);
    }
  }

  public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
  }

  public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
  }
}

-----------------------------------------------------------------------------------

try {
            javax.swing.JPanel panel = new javax.swing.JPanel(new java.awt.BorderLayout());
            panel.setSize(588, 600);
            javax.swing.JTextArea area = new javax.swing.JTextArea();
            area.setBounds(0, 0, 600, 500);
            panel.add(area);
            //java.awt.Font font = new java.awt.Font("Arial Black",java.awt.Font.BOLD,20);
            area.append("\n");
            area.append("============================================================================================\n");

            //area.append("\tPatient Name\t\tCost\tReffered BY\t\tDate\t\n");
            area.append("\tIndex  \t\tName \tQuantity \tAmount\t\n ");
            area.append("===================================================================================================\n");

            System.out.println("Row Count is :" + tm.getRowCount());
            for (int i = 0; i < tm.getRowCount(); i++) {
                area.append("\t" + tblRecord.getValueAt(i, 0) + "\t\t" + tblRecord.getValueAt(i, 1) + "\t" + tblRecord.getValueAt(i, 3) + "\t" + tblRecord.getValueAt(i, 5) );
                area.append("\n--------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
            }
         
            area.append("\n\tTotal :\t"+lblTotal.getText()+"\tDiscounte :\t"+tfDisc.getText());
             area.append("\n\tRecieve :\t"+tfRecieve.getText()+"\tFinal Total :\t"+lblFinal.getText());
             area.append("\n\tBalance :\t"+lblBal.getText());


            PrintUtilities pu = new PrintUtilities(area);
            panel.setVisible(true);
            pu.print();
        } catch (Exception e) {
            System.out.println("Error in Print Button :" + e);
        }
link

answered 2011-08-13 at 03:35:57

Aurii's gravatar image

Aurii

It prints for me without problems of HP laser printer
which is my default printer.

In order not to show print dialog, just comment out
this line:

if (printJob.printDialog())

(see below).

It will print directly.

In order to adjust width, etc,
see
http://java.sun.com/products/java-media/2D/forDevelopers/sdk12print.html
with many examples there with detailed explanation



1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
package tushar;

import javax.swing.*;



import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

public class PrintUtilities implements Printable {
  private Component componentToBePrinted;

  public static void printComponent(Component c) {
    new PrintUtilities(c).print();
  }

  public PrintUtilities(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
  }

  public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    //if (printJob.printDialog())
      try {
        printJob.print();
      } catch(PrinterException pe) {
        System.out.println("Error printing: " + pe);
      }
  }

  public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
      return(NO_SUCH_PAGE);
    } else {
      Graphics2D g2d = (Graphics2D)g;
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
      disableDoubleBuffering(componentToBePrinted);
      componentToBePrinted.paint(g2d);
      enableDoubleBuffering(componentToBePrinted);
      return(PAGE_EXISTS);
    }
  }

  public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
  }

  public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
  }


    public static void main(String[] args) {
        try {
                    javax.swing.JPanel panel = new javax.swing.JPanel(new java.awt.BorderLayout());
                    panel.setSize(588, 600);
                    javax.swing.JTextArea area = new javax.swing.JTextArea();
                    area.setBounds(0, 0, 600, 500);
                    panel.add(area);
                    //java.awt.Font font = new java.awt.Font("Arial Black",java.awt.Font.BOLD,20);
                    area.append("
");
                    area.append("============================================================================================
");

                    //area.append("	Patient Name		Cost	Reffered BY		Date	
");
                    area.append("	Index  		Name 	Quantity 	Amount	
 ");
                    area.append("===================================================================================================
");

                  //  System.out.println("Row Count is :" + tm.getRowCount());
                    //for (int i = 0; i < tm.getRowCount(); i++) {
                        //area.append("	" + tblRecord.getValueAt(i, 0) + "		" + tblRecord.getValueAt(i, 1) + "	" + tblRecord.getValueAt(i, 3) + "	" + tblRecord.getValueAt(i, 5) );
                        //area.append("
--------------------------------------------------------------------------------------------------------------------------------------------------------------
");
                        area.append("	  OneOneOne 		TwoTwoTwo	ThreeThreeThree	FourFour" );
                        area.append("
--------------------------------------------------------------------------------------------------------------------------------------------------------------
");


                   // }

                //    area.append("
	Total :	"+lblTotal.getText()+"	Discounte :	"+tfDisc.getText());
                 //    area.append("
	Recieve :	"+tfRecieve.getText()+"	Final Total :	"+lblFinal.getText());
                  //   area.append("
	Balance :	"+lblBal.getText());


                    PrintUtilities pu = new PrintUtilities(area);
                    panel.setVisible(true);
                    pu.print();
                } catch (Exception e) {
                    System.out.println("Error in Print Button :" + e);
                }

    }
}
link

answered 2011-08-19 at 23:32:18

for_yan's gravatar image

for_yan

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×141
×1

Asked: 08/09/2011 03:18

Seen: 385 times

Last updated: 11/22/2011 02:03