import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Gui extends JFrame
{
private static final long serialVersionUID = 1L;
JLabel lblJob;
JTextField txtJob;
JLabel lblQty;
JTextField txtQty;
JButton btnPrint;
static Font barcode = new Font("Free 3 of 9 Extended", Font.PLAIN, 12);//barcode font
public Gui()
{
super("Test");
setSize(300, 200);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
getContentPane().setBackground(new Color(0xee,0xee,0xee));
lblJob = new JLabel("Job:");
lblJob.setBounds(25, 25, 50, 25);
lblJob.setFont(new Font("Tahoma", Font.PLAIN, 16));
add(lblJob);
txtJob = new JTextField();
txtJob.setBounds(75, 25, 100, 25);
add(txtJob);
lblQty = new JLabel("Qty:");
lblQty.setBounds(25, 65, 50, 25);
lblQty.setFont(new Font("Tahoma", Font.PLAIN, 16));
add(lblQty);
txtQty = new JTextField();
txtQty.setBounds(75, 65, 100, 25);
add(txtQty);
btnPrint = new JButton("Print");
btnPrint.setBounds(75, 105, 100, 25);
btnPrint.addActionListener(new btnPrintAction(this));
add(btnPrint);
}
public static class btnPrintAction implements ActionListener, Printable
{
SQLDatabase database;
int qty;
Gui x;
int numPrinted = 0;
public btnPrintAction(Gui x)
{
this.x = x;
}
public int print(Graphics gx, PageFormat pf, int page) throws PrinterException
{
if(page > 0)
{
return NO_SUCH_PAGE;
}
System.out.println("Page check");
Graphics2D g = (Graphics2D)gx;
g.translate(pf.getImageableX(), pf.getImageableY());
g.setFont(barcode);
for(int r = 0; r < 7; r++)
{
for(int c = 0; c < 2; c++)
{
if(numPrinted >= qty)
{
return PAGE_EXISTS;
}
else
{
try {
g.drawString(database.rs.getString(1), 10 + 300*c, (int) (80 + r*57));//r*97
++numPrinted;
System.out.println(numPrinted);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
return PAGE_EXISTS;
}
public void actionPerformed(ActionEvent ae) {
numPrinted = 0;
try
{
qty = Integer.parseInt(x.txtQty.getText());
}catch(Exception ex){
JOptionPane.showMessageDialog(new JFrame(), "Please enter a valid number","Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
database = new SQLDatabase(x);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
if (job.printDialog() == true)
{
try
{
while(qty > numPrinted)
job.print();
}catch (PrinterException ex){
ex.printStackTrace();
}
}
try {
database.st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
database.conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
new Gui().setVisible(true);
}
}