Search code examples
javaeclipseuser-interfacejbutton

How can I make JFrame close a external window?


I was wondering if anyone knew how I could make JFrame close an external window. For example a .bat file. My GUI window looks like this, ( https://cdn.discordapp.com/attachments/339245512647770112/372569903070183425/unknown.png ) I want to make where it says "Close" close the file that I opened with the "Run" button.

        btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
    btnNewButton.setBounds(375, 190, 120, 40);
    frame.getContentPane().add(btnNewButton);

    JButton btnStart = new JButton("Run");
    btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    btnStart.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            try {
                Desktop.getDesktop().open(new File("C:\\Users\\User\\Desktop\\Discord-Selfbot-master\\self-bot.bat"));
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    });

Is it possible to make it so that I can close the file using the same or similar code? If you have any information about this then I would be happy to hear it.

Thank you for your time and consideration. -Brand0n


Solution

  • You could use the ProcessBuilder to start your program. The returned process can then be used to terminate the program.

    Here is a full working code

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.concurrent.TimeUnit;
    
    import javax.swing.*;
    
    public class TestFrame {
    
    public static void testWithoutUI(String s[]) {
        ProgramRunner programRunner = new ProgramRunner("notepad.exe");
        programRunner.start();
    
        System.out.println("waiting");
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    
        programRunner.getProcess().destroyForcibly();
    
        System.out.println("done");
    
    }
    
    public static void main(String s[]) {
    
        JFrame frame = new JFrame("JFrame Example");
    
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
    
        JLabel label = new JLabel("Self-Bot");
    
    
        ProgramRunner programRunner = new ProgramRunner("notepad.exe");//C:\\Users\\User\\Desktop\\Discord-Selfbot-master\\self-bot.bat
    
    
    
        JButton btnStart = new JButton("Run");
        btnStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnStart.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                programRunner.start();
            }
        });
    
        JButton buttonClose = new JButton();
        buttonClose.setText("close");
    
        buttonClose.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                programRunner.getProcess().destroyForcibly();
            }
        });
    
        panel.add(label);
        panel.add(btnStart);
        panel.add(buttonClose);
    
        frame.add(panel);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    }
    
    
    
    class ProgramRunner extends Thread{
    private String pathToFile = null;
    private Process process = null;
    
    public ProgramRunner(String pathToFile) {
        this.pathToFile = pathToFile;
    }
    
    @Override
    public void run() {
        try {
            startProgram();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private void startProgram() throws IOException {
        process = new ProcessBuilder(pathToFile).start();
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
          System.out.println(line);
        }
    }
    
    public Process getProcess() {
        return process;
      }
    }