|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-21-2008, 03:53 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 5
|
|
|
calling a public void method from a class button
hi guys i am working on swing classes, and what i am trying to do is call the printData() class and its method printStuff() from a touch of a button but when i excute it i get an error...can any body help me out or give me some advice....i will appreciate any help given ....tc
class Printdata2{
public Printdata2()
{
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 Ticket";
Statement stmt = connect.createStatement();
String linebreak = System.getProperty("line.separator");
ResultSet rset = stmt.executeQuery("SELECT tnum, timein, timeout FROM Ticket");
String str = "";
while (rset.next()) {
str += linebreak + "Ticket Number: "+ rset.getObject(1)+ linebreak +"Customer Time In: "+ rset.getObject(2)+ linebreak +"Customer Time Out: "+ rset.getObject(3)+ linebreak+ linebreak;
}
byte buf[] = str.getBytes();
OutputStream fp = new FileOutputStream("tickets.txt");
fp.write(buf);
fp.close();
rset.close();
stmt.close();
connect.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws Exception {
Printdata2 fr = new Printdata2();
}
public void printStuff(String args[]) throws Exception{
String filename = ("tickets.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);
}
}
}
this class or button that i want to call class Printdata() and its method printStuff()..
class B8 implements ActionListener {
public void actionPerformed(ActionEvent e) {
Printdata2 frame = new Printdata2();
Printdata2.printStuff();
}
the errors i get are below:::::
C:\Users\omar\Desktop>javac JFrameApplication.java
JFrameApplication.java:130: printStuff(java.lang.String[]) in Printdata2 cannot
be applied to ()
Printdata2.printStuff();
^
Note: JFrameApplication.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: JFrameApplication.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
C:\Users\omar\Desktop>
|
|

05-21-2008, 07:38 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
|
It says that you have to pass an array of Strings in printStuff() method when invoking on it....
try to compile those java files with -Xlint,
a detailed info should be there and guide you where are those warnings came from....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-21-2008, 02:40 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 5
|
|
heres the error i get ....
C:\Documents and Settings\10294455\Desktop>javac JFrameApplication.java -Xlint
frame.printStuff();
^
1 error
C:\Documents and Settings\10294455\Desktop>
|
|

05-21-2008, 04:21 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 5
|
|
i got it working !!!! theres only one problem see below.....
class B8 implements ActionListener {
public void actionPerformed(ActionEvent e) {
Printdata2 frame = new Printdata2();
}
class Printdata2{
public Printdata2()
{
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 Ticket";
Statement stmt = connect.createStatement();
String linebreak = System.getProperty("line.separator");
ResultSet rset = stmt.executeQuery("SELECT tnum, timein, timeout FROM Ticket");
String str = "";
while (rset.next()) {
str += linebreak + "Ticket Number: "+ rset.getObject(1)+ linebreak +"Customer Time In: "+ rset.getObject(2)+ linebreak +"Customer Time Out: "+ rset.getObject(3)+ linebreak+ linebreak;
String filename = ("tickets.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);
}
}
}
byte buf[] = str.getBytes();
OutputStream fp = new FileOutputStream("tickets.txt");
fp.write(buf);
fp.close();
rset.close();
stmt.close();
connect.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws Exception {
Printdata2 fr = new Printdata2();
}
i wrote this bit of code in constructer bit and deleted printStuff().. but when the code reaches this bit of code it executes, and does'nt stop excuting it...
String filename = ("tickets.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);
}
|
|

05-23-2008, 03:05 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 5
|
|
|
i solved it thax ...aniwais
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|