Link to home
Start Free TrialLog in
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
Avatar of for_yan
for_yan
Flag of United States of America image



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
Avatar of CEHJ
You're probably better off trying to print headlessly:

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

(search on print)
Avatar of Aurii
Aurii

ASKER

Thank you for reply. I tried these things but its not worked for me.  Please suggest me any solution.
Please post the code you used and show the results, plus any error messages in full
Avatar of 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
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial