hi guys,
i have created a class which has a couple of buttons and each button calls a public class, class B4 is a button and it calls up the Printdata class,
the Printdata class, then gets data from the database carpark and stores it into a file.txt as B4 button is pressed,
also within the Printdata.java i have a main that should bring up a print dialog to print the file.txt, but for some reason this part of the code is'nt excuted...can anyone advice..it will be appreciated very much...thx
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.math.*;
import javax.swing.border.*;
import javax.print.*;
import javax.print.attribute.*;
public class JFrameApplication extends JFrame {
public JFrameApplication (String title){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
System.out.println("Car Park Management");
JFrameApplication f = new JFrameApplication ("JFrame Application");
f.setSize(260,350);
f.setTitle("Management Screen");
Container content = f.getContentPane();
content.setBackground(Color.lightGray);
content.setLayout(new FlowLayout());
JButton b4 = new JButton(" Print all Members Details ");
content.add(b4);
b4.addActionListener(new B4());
f.show();
}
} // end of JFrameApplication class
class B4 implements ActionListener {
public void actionPerformed(ActionEvent e) {
Printdata frame = new Printdata();
frame.printStuff();
}
}
class Printdata extends JFrame {
public Printdata()
{
try
{
// Connect to the Database
//connection object created using DriverManager class
//carpark is the name of the database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connect =DriverManager.getConnection("jdbc:odbc:carpark");
// Read data from a table
String sql = "SELECT * FROM Member";
Statement stmt = connect.createStatement();
String linebreak = System.getProperty("line.separator");
ResultSet rset = stmt.executeQuery("SELECT userid, cname, cadd, cardno, pin FROM Member");
String str = "";
while (rset.next()) {
str += linebreak + "Customers User ID: "+ rset.getObject(1)+ linebreak "Customer Name: " rset.getObject(2)+ linebreak "Customer Address: " rset.getObject(3)+ linebreak+"Credit Card Number: "rset.getObject(4) linebreak+"Customer Pin: "rset.getObject(5) linebreak+ linebreak;
}
byte buf[] = str.getBytes();
OutputStream fp = new FileOutputStream("file.txt");
fp.write(buf);
fp.close();
rset.close();
stmt.close();
connect.close();
}
catch(Exception e) {
e.printStackTrace();
}
this.setSize(450,260);
this.setLocation(200,200);
}
public void printStuff(String args[]) throws Exception {
Printdata fr = new Printdata();
String filename = ("file.txt"); // THIS IS THE FILE I WANT TO PRINT
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; // MY FILE IS .txt TYPE
PrintService printService[] =
PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
Thread.sleep(10000);
}
System.exit(0);
}
}
thanks any help will be appreciated
tc