Results 1 to 5 of 5
  1. #1
    Join Date
    May 2008
    Posts
    5
    Rep Power
    0

    Default 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

    Java Code:
    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()..

    Java Code:
    class B8 implements ActionListener {
    		public void actionPerformed(ActionEvent e) {	
    			Printdata2 frame = new Printdata2();
    				Printdata2.printStuff();
    		}





    the errors i get are below:::::

    Java Code:
    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>

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    10

    Default

    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....
    freedom exists in the world of ideas

  3. #3
    Join Date
    May 2008
    Posts
    5
    Rep Power
    0

    Default

    heres the error i get ....

    Java Code:
    C:\Documents and Settings\10294455\Desktop>javac JFrameApplication.java -Xlint
                                    frame.printStuff();
                                         ^
    1 error
    
    
    C:\Documents and Settings\10294455\Desktop>

  4. #4
    Join Date
    May 2008
    Posts
    5
    Rep Power
    0

    Default

    i got it working !!!! theres only one problem see below.....


    Java Code:
    class B8 implements ActionListener {
    		public void actionPerformed(ActionEvent e) {	
    			Printdata2 frame = new Printdata2();		
    		}

    Java Code:
    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...

    Java Code:
     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);
        }

  5. #5
    Join Date
    May 2008
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 1
    Last Post: 05-21-2008, 05:40 AM
  3. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 07:24 PM
  4. public method
    By dirtycash in forum New To Java
    Replies: 4
    Last Post: 11-21-2007, 08:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •