Results 1 to 2 of 2
  1. #1
    JavaSwingUser is offline Member
    Join Date
    Feb 2015
    Posts
    3
    Rep Power
    0

    Default GUI becomes unresponsive after calling another process

    Hello users,

    So I have built a GUI using Java Swing which has a number of input fields. It has 2 Buttons Ok and Cancel.
    1. When user clicks OK :
    (first disable the OK button then..)
    1.1 I am supposed to call an an exe and pass all the input parameters to it. (I am using Process p = Runtime.getRuntime().exec(command);)
    1.2 This exe writes a file called start.txt / finish.txt to a folder (the outputFolder) when the exe has started running/finished running
    1.3 When I see start.txt show message "process started".
    1.4 When finish.txt is written I have to enable OK button.

    To do this, in the actionListener for OK button I call a method runTheEXE() and then check the existence of files in a while loop in my actionListener.

    2. When the user clicks on cancel:

    2.1 I have to write a file cancel.txt in the outputFolder.

    Problem:

    When I click ok, process starts (and the while loops monitor for the files), but I am unable to click on Cancel (GUI becomes unresponsive). I am completely newbie to Java Swing. Can someone please suggest me a strategy to go about what Im trying to do. The exe is non-editable. After running the process, I need to send back control to my GUI and also continue monitoring for the files. I will appreciate any help and let me know if I can present my question better. Thank you!

    This is what I have so far:

    public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
    frame = new MyProgram();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    Inside MyProgram() constructor:


    btnOk.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    btnOk.setEnabled(false);
    runMYEXE(inp1,inp2);
    boolean startFileExistsFlag =checkFileExists(startFilePath);

    while(!startFileExistsFlag)
    {
    startFileExistsFlag = checkFileExists(startFilePath);
    if(!startFileExistsFlag)
    {
    try {
    Thread.sleep(1000);
    } catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }
    else
    {
    System.out.println("Start File has been written");
    }
    }
    boolean finishFileExistsFlag =checkFileExists(finishFilePath);
    if(startFileExistsFlag==true) // start.txt is written
    {
    while(!finishFileExistsFlag)
    {
    finishFileExistsFlag = checkFileExists(finishFilePath);
    if(!finishFileExistsFlag)
    {
    try {
    Thread.sleep(30000);
    } catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }
    else
    {
    System.out.println("Process has finished. Finish.txt has been written");
    }
    }
    }
    if(finishFileExistsFlag)
    {
    btnOk.setEnabled(true);
    }


    __________________________________________________ __________________________________________________ ________________________________________________


    btnCancel.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {



    File cancel = new File(txtFieldOutputFolderPath.getText()+"\\cancel. txt");
    try {
    cancel.createNewFile();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }


    }
    });

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: GUI becomes unresponsive after calling another process

    When posting code, please use the code tags to preserve formatting, and post your code as an MCVE so we can run it. But without really looking at it, you need to put any processing on another Thread, that way the GUI Event Thread (the EDT) can still respond to user input and display stuff.

    Googling "Swing concurrency" will return a ton of results.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Unresponsive keybindings
    By Insomniac.. in forum AWT / Swing
    Replies: 5
    Last Post: 07-21-2013, 06:50 PM
  2. Swing button unresponsive
    By Orlan of Solace in forum AWT / Swing
    Replies: 3
    Last Post: 04-06-2013, 03:16 AM
  3. Replies: 2
    Last Post: 11-06-2009, 02:56 PM
  4. Process name
    By logicbug in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-16-2009, 02:11 PM
  5. [SOLVED] Gui becomes unresponsive!
    By FezKazi in forum AWT / Swing
    Replies: 10
    Last Post: 03-04-2009, 04:47 AM

Tags for this Thread

Posting Permissions

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