Bring control from application to java frame

北战南征 提交于 2019-12-24 07:13:01

问题


I use a java program for communication between a arduino board and a scratch file. The communication happen well. I use a user interface to start the communication where i have buttons called

connect close and minimize

When the user clicks the connect button code will check the value in combo box and accordingly it opens the scratch file.

Once the connect button is clicked the control moves to the scratch application. After completing my work when i tried closing the scratch. My scratch application closes as expected but the control does not return to the user interface because of which i am not able to close the application and i close it in net beans forcefully. In the output screen i don't see build successful and instead i get build stopped. That is my process works perfectly until i give connect but once the button is pressed it is hanged up some where.

I tried making it as a jar file and running it in a different machine at that time i use end task in task manager to close the application.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

if("Disconnect".equals(jButton1.getText()))
{
    System.exit(0);
}

if(jComboBox2.getSelectedItem()==null)
{
    System.out.println("Select one port");
}
else
{           
    Runtime r = Runtime.getRuntime();

    try {
        //this.hide();
        //p = r.exec("C:\\Program Files\\Scratch 2\\Scratch 2.exe     C:\\Users\\Admin\\Desktop\\fwdbckpwm12.sb2");

        p = Runtime.getRuntime().exec("C:\\Program Files\\Scratch 2\\Scratch   2.exe C:\\Users\\Admin\\Desktop\\scratch files new.sb2");

        //Runtime.getRuntime().exec("taskkill /F /IM <p>.exe");            
        //p.destroy();
        //r.exec("C:\\Windows\\notepad.exe C:\\Windows\\ss.txt");
        //this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        A4S a4sObj = new A4S(new String[] {jComboBox2.getSelectedItem().toString()});  //defaultline

        //A4S a4sObj = new A4S(new String[]{"COM16"});  //addedline
        //r.gc();
        //this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    } catch (IOException ex) {
        Logger.getLogger(serialportselection.class.getName()).log(Level.SEVERE, null, ex);
    } 
    finally{
        //p.destroy();
        //System.gc(); 
   // }

}            

Here is the code i tried. But none seems to work.


回答1:


  1. Move all Process related work into separate Thread.
  2. Use waitFor method to recognise Process end - then you are free to exit your app.



回答2:


As I can understood you used SWING for creating UI. You can set

 yourFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);

for your frame. This must help.



来源:https://stackoverflow.com/questions/36832094/bring-control-from-application-to-java-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!