0

I have a form with an open and a back button. I open my batch file through the open button, while the batch file is executing, the other buttons are disabled. I want to enable these buttons. Please help me.

Run batch file code:

private void openActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    // back.setEnabled(true);
    String filename = "C:\\JMeter Project\\jakarta-jmeter-2.5.1\\bin\\jmeter.bat";
    String command = filename;
    Runtime runtime = Runtime.getRuntime();
    try {
        Process process = runtime.exec(command);
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = reader.readLine();
        while (line != null) {
            System.out.println(line);
            line = reader.readLine();
            System.out.println(line);
        }
        back.setEnabled(true);
        JOptionPane.showMessageDialog(null, "Batch file executed successfully.....!!!!");
    } catch (IOException e) {

        JOptionPane.showMessageDialog(null, "Batch file execution failed.");
    }
    // Form f=new Form();
    // back.action(f.setVisible(true),null);   

}

back button code:

private void backActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    close();
    sms_sound s = new sms_sound();
    //s.setVisible(true);
    Form f = new Form();
    f.setVisible(true);
    //  back.setEnabled(true);
}
GraphicsMuncher
  • 4,583
  • 4
  • 35
  • 50
Richa
  • 11
  • 6
  • I should point out that your other problem is the back button will only be re-enabled if the process doesn't fail...don't know if this the functionality you want or not, but it looks fishy to me – MadProgrammer Feb 28 '13 at 06:56

1 Answers1

2

You're blocking the event dispatching thread, which is preventing it from processing any repaint requests.

You should move you "batch" code to a background thread, something like a SwingWorker would be excellent for this problem.

Added example

public class BatchRunner extends SwingWorker<Integer, String> {

    @Override
    protected Integer doInBackground() throws Exception {
        String filename = "C:\\JMeter Project\\jakarta-jmeter-2.5.1\\bin\\jmeter.bat";
        String command = filename;
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            publish(line);
        }
        return process.exitValue();
    }

    @Override
    protected void process(List<String> chunks) {
        // You can process the output produced
        // the doBackgroundMethod here within the context of the EDT
    }

    @Override
    protected void done() {
        try {
            Integer result = get();
            if (result == 0) {
                JOptionPane.showMessageDialog(null, "Batch file executed successfully.....!!!!");
            } else {
                JOptionPane.showMessageDialog(null, "Batch file returned an exit value of " + result);
            }
        } catch (InterruptedException | ExecutionException | HeadlessException interruptedException) {
            JOptionPane.showMessageDialog(null, "Batch file execution failed.");
        }
        back.setEnabled(true);
    }
}

When you're ready to execute your batch program, simply do something like...

back.setEnabled(false);
new BatchRunner().execute();
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366