0

Pretty much what I'm trying to do is make a custom installer, I have buttons and that working fine but I want to run another class called CopyDir.java when I click on the button so that it copies the necessary files to the correct directory. Problem is, is that I'm a bit stumped on how to do this.

public class Frame extends JFrame implements ActionListener {
JPanel pane = new JPanel();
JButton PC = new JButton("Install Mod (PC)");
JButton Steam = new JButton("Install Mod (Steam)");
JLabel Text = new JLabel("Welcome to the BTD 5 Mod Installer");
JLabel Text2 = new JLabel("Click on the button that matches your version of BTD 5");
JLabel Text3 = new JLabel("To install it for the version that you are using");
JLabel Text4 = new JLabel("© Nixxx60/Nanikos");
Frame() {
    super("BTD 5 Mod Installer");
    setBounds(100, 100, 400, 150);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container con = this.getContentPane();
    con.add(pane);
    PC.setMnemonic('P');
    PC.addActionListener(this);
    pane.add(PC);
    PC.requestFocus();
    con.add(pane);
    Steam.setMnemonic('P');
    Steam.addActionListener(this);
    pane.add(Steam);
    Steam.requestFocus();
    setVisible(true);
    pane.add(Text);
    pane.add(Text2);
    pane.add(Text3);
    pane.add(Text4);
}

public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    if (source == PC) {
        JOptionPane.showMessageDialog(null, "Mod has been installed on PC/Cracked Edition!", "BTD 5 Installer",
            JOptionPane.PLAIN_MESSAGE);
        setVisible(true);
    }
    if (source == Steam) {
        JOptionPane.showMessageDialog(null, "Mod has been installed for Steam Edition!", "BTD 5 Installer",
            JOptionPane.PLAIN_MESSAGE);
        setVisible(true);
    }
}
public static void main(String args[]) {
    new Frame();
}
}

Also, here is the code for the "CopyDir.java" Class.

package Nanikos.BTD5.Main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class CopyDir {

    public static void main(String args[]) throws Exception
    {
        copyFiles(new File("C:\\Users\\User\\Desktop\\BTD5 Mod Installer\\Mod Files\\PC Assets"),new File("C:\\Program Files (x86)\\Steam\\steamapps\\common\\BloonsTD5"));
        System.out.println("Files Copied");
    }

    public static void copyFiles(File src,File des) throws Exception
    {
        if(src.isDirectory())
        {
            if(!des.exists()) des.mkdir();
            String [] filePaths=src.list();
            for(String filePath: filePaths)
            {
                File srcFile =new File(src, filePath);
                File desFile =new File(des, filePath);
                copyFiles(srcFile,desFile);
            }
        }
        else
        {
            FileInputStream from =null;
            FileOutputStream to =null;

            from = new FileInputStream(src);
            to = new FileOutputStream(des);
            byte [] buffer=new byte[4096];
            int byteReads;

            while( (byteReads=from.read(buffer))!=-1 )
            {
                to.write(buffer,0,byteReads);
            }

            from.close();
            to.close();
        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Hey Nicholas, the problem is not clear to me. To react on button clicks, you have to write `ActionListener`, as you did. If you want to call another class make call the constructor followed by a method call. `new CopyDir().doSomething()`. If you want to start a long running task please see the documentation about [Threads in Swing] (https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) – Dangermouse Aug 19 '16 at 08:06
  • Also: `if (source == PC) {...` should be `if (source.equals(PC)) {...`. And you might want to use an `if (...) { ... } else if (...) {...}`-construct. – pzaenger Aug 19 '16 at 08:56
  • https://stackoverflow.com/questions/59431204/i-am-new-to-programming-need-guidance can someone have a look into my request – Khan Dec 20 '19 at 20:51

1 Answers1

0

What you want is to launch your class as a Thread

To launch your class CopyDir as a thread, make it implement Thread, change your main method signature to this :

public void run() {
  //Your code
}

Also, to pass parameters to your thread, add a constructor in your CopyDir class that takes the parameters you have and stores them as attributes, to be able to get it from your method.

Then, to launch the thread from your event listeners :

 CopyDir myCopyThread = new CopyDir(inputPath,outputPath);
 myCopyThread.start();

This code will create a thread that starts running CopyDir in the run() method

Dijdkay
  • 56
  • 1
  • 5
  • Worked perfectly, just moved `CopyDir myCopyThread = new CopyDir(); myCopyThread.start();` under my `if (source == PC) { }` All I need to do is make another class exactly like CopyDir and call it CopyDir1 then do the same thing but put this other one under `if (source == Steam) {}` since the CopyDir1 will be for other directory files. – Nicholas Blyde Aug 20 '16 at 04:55
  • Ok, there is one more problem. C:\\Users\\User\\Desktop\\BTD5 Mod Installer\\Mod Files\\PC Assets How could I get it to detect the User, since not everyone installing this will have a User named User. I know there is public static void main(){ String username = System.getProperty("user.name"); System.out.println("username = " + username); } } but I want it to take that user.name and insert it into C:\\Users\*********\ – Nicholas Blyde Aug 20 '16 at 06:18
  • Try to look at [this](http://stackoverflow.com/questions/9677692/getting-my-documents-path-in-java) – Dijdkay Aug 22 '16 at 06:11