Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Cleaner Program Help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Krocker




PostPosted: Sat Dec 31, 2011 12:35 pm   Post subject: Cleaner Program Help

hey guys, ok so im creating a program which delete specific folder and whats in them. The problem im having is that i have 2 classes ~ one has my guis and buttons, the other has the deleting process. What i dont know is how to make it so that when the clean button is pressed, it class the delete method from the other class. Heres my codes


Main Process (Guis/Main Interface)

code:


package kcleaner;


import java.awt.event.*;
import javax.swing.*;
import java.io.*;


public class Test extends JFrame implements ActionListener
{
    public JFrame f1 = new JFrame ("Frame");
    public JPanel p1 = new JPanel ();
    public JButton b1 = new JButton ("Clean");
    public JButton b2 = new JButton ("Close");
    public Test ()

    {
        b1.addActionListener (this);
        b2.addActionListener (this);
        f1.setContentPane (p1);
        f1.setSize (400, 400);
        p1.add (b1);
        p1.add (b2);
        f1.setVisible (true);

    }


    public void actionPerformed (ActionEvent e)
    {
        if (e.getSource () == b1)
        {
            JOptionPane.showMessageDialog (null, "KCleaner is Done!");
         
        }
        else if (e.getSource() == b2)
        {
            JOptionPane.showMessageDialog (null, "Thank You For Using KCleaner");
            f1.setVisible (false);
        }
    }


    public static void main (String args[])
    {
        new Test ();
    }
}



dProcess (Deleting Class)
code:


package kcleaner;

import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class dProcess
{
    public static void main (String[] argv) throws Exception
    {
       boolean file = directory (new File ("C:\\Nexon/Combat Arms/BlackCipher"));
       
        if (file == false)
        {
            JOptionPane.showMessageDialog (null, "Directory does not exist.");
        }
        else
        {
        }

    }


    public static boolean directory (File dir)
    {

        if (dir.isDirectory ())
        {
            String[] children = dir.list ();
            for (int i = 0 ; i < children.length ; i++)
            {
                boolean success = directory (new File (dir, children [i]));
                if (!success)
                {
                    return false;
                }
            }
        }
        return dir.delete ();

    }
}
Sponsor
Sponsor
Sponsor
sponsor
ProgrammingFun




PostPosted: Sat Dec 31, 2011 12:47 pm   Post subject: RE:Cleaner Program Help

If both classes are in the same folder, they can communicate with each other just as they would if they were in the same program.
Another way to connect them can be extends
Krocker




PostPosted: Sat Dec 31, 2011 3:03 pm   Post subject: RE:Cleaner Program Help

ok, but thats the thing, i dont know to to communicate with each other. Please explain and give me an example to follow. THX ALOT
ProgrammingFun




PostPosted: Sat Dec 31, 2011 4:32 pm   Post subject: RE:Cleaner Program Help

First off, you need to make sure that one of you classes is finished in all of its coding. This is normally the class that does not contain the main program, for you, you would have to comple dProcess first. Next, place the .class that is generated into the same folder as your other java file if it is not already there. After this point, you can call any method in the compiled class as you would call it normally. This is done as follows:

dProcess file
Java:

public class AnExample
{
     public static void MyMethod () //An example method
     {
          //Whatever this does
     }
}


My Main Program Class
Java:

public class FinalProg
{
     public static void main (String[] args)  //Main Program
     {
          AnExample.MyMethod(); //Calling the method in the other class by classname.methodname(parameter)
     }
}


Hope this helps, it is explained in more detail here: http://www.roseindia.net/java/javascript-array/call-method-another-class.shtml
If you still do not understand it, I would suggest just sticking with one class for now.
Krocker




PostPosted: Sat Dec 31, 2011 7:20 pm   Post subject: RE:Cleaner Program Help

ok so i followed what u said and im getting an error saying that :

No application overload for the method named "main" was found in type "kcleaner.dProcess". Perhaps you wanted overload version "void main (java.lang.String[] argv) throws java.lang.Exception;" instead?


I have no idea what that means, so heres the code for that error. Note that the method is called when a button is pressed in the main program.

public void actionPerformed (ActionEvent e)
{
dProcess method = new dProcess ();
if (e.getSource () == b1)
{

JOptionPane.showMessageDialog (null, "KCleaner is Done!");
method.main ();


}
else if (e.getSource () == b2)
{
JOptionPane.showMessageDialog (null, "Thank You For Using KCleaner");
f1.setVisible (false);
}
}


public static void main (String args[])
{
new Test ();
}
}
ProgrammingFun




PostPosted: Sat Dec 31, 2011 7:48 pm   Post subject: Re: RE:Cleaner Program Help

Krocker @ Sat Dec 31, 2011 7:20 pm wrote:

public static void main (String args[])

This should be:
Java:

public static void main (String[] args)


EDIT: Also, why do you have a main method in each class? You only need one of those, the main method is supposed to be the one that initializes everything.
Krocker




PostPosted: Sun Jan 01, 2012 10:23 am   Post subject: RE:Cleaner Program Help

ok, but the error is coming from the "method.main ();"
ProgrammingFun




PostPosted: Sun Jan 01, 2012 12:12 pm   Post subject: Re: RE:Cleaner Program Help

Krocker @ Sun Jan 01, 2012 10:23 am wrote:
ok, but the error is coming from the "method.main ();"

What does your "main" look like in you "method" class?
Sponsor
Sponsor
Sponsor
sponsor
Krocker




PostPosted: Sun Jan 01, 2012 1:10 pm   Post subject: Re: RE:Cleaner Program Help

ProgrammingFun @ Sun Jan 01, 2012 12:12 pm wrote:
Krocker @ Sun Jan 01, 2012 10:23 am wrote:
ok, but the error is coming from the "method.main ();"

What does your "main" look like in you "method" class?


Main Method:

code:

 public static void main (String[] arg) throws Exception
    {
     String drive = JOptionPane.showInputDialog(null, "Please Enter Your Main Hard Drive Letter", "Hardrive", JOptionPane.QUESTION_MESSAGE );
     // String user = JOptionPane.showInputDialog(null, "Please Enter The User Accout Name", "User Account", JOptionPane.QUESTION_MESSAGE );
     
     String folder = drive.toUpperCase()+":\\Nexon/Combat Arms/BlackCipher";
     
     
     boolean file = directory (new File (folder));
       
        if (file == false)
        {
            JOptionPane.showMessageDialog (null, "Directory does not exist.");
        }
        else
        {
        }

    }
ProgrammingFun




PostPosted: Sun Jan 01, 2012 1:28 pm   Post subject: RE:Cleaner Program Help

http://en.wikipedia.org/wiki/Main_function#Java
Krocker




PostPosted: Sun Jan 01, 2012 5:50 pm   Post subject: RE:Cleaner Program Help

... nice tutorial, but that doesnt help me in my case
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: