Avatar of Aurii
Aurii
 asked on

Print customized sized print on dot matrix printer

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
Editors IDEsJava

Avatar of undefined
Last Comment
for_yan

8/22/2022 - Mon
for_yan



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
CEHJ

You're probably better off trying to print headlessly:

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

(search on print)
Aurii

ASKER
Thank you for reply. I tried these things but its not worked for me.  Please suggest me any solution.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
CEHJ

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

ASKER
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);
        }
ASKER CERTIFIED SOLUTION
for_yan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question