Swing message doesn't get displayed until after Runtime.getRuntime().exec() finishes execution

后端 未结 2 1031
有刺的猬
有刺的猬 2020-12-02 03:31

I am new to Swing. I am trying to create a swing wrapper to allow the user to browse and select a folder, and that folder path is used as a command line parameter to a cons

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 03:37

    It seems you're doing it all in one thread.

    Use event dispatch thread to call your gui code.

    private void onLaunchProgram(ActionEvent evt) {
            String strExecutableFilename = "MyExecutableProgam";
            String strSourceFolder = txtFolder.getText();
            String strCommand = strExecutableFilename + " " + strSourceFolder;
            ImageIcon icon = new ImageIcon("clock.gif");
            javax.swing.SwingUtilities.invokeLater(
                new Runnable() {
                     public void run() {
                         lblMessage.setText("Processing");
                         lblPic.setIcon(icon);
                     }
                });
    
            try {
                Process procCommand = Runtime.getRuntime().exec(strCommand);
                try {
                    procCommand.waitFor();
                } catch (InterruptedException exception) {
                    exception.printStackTrace();
                } finally {
                }
    
                javax.swing.SwingUtilities.invokeLater(
                    new Runnable() {
                        public void run() {
                          lblMessage.setText("Finished");
                        }
                     });
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
        }
    

提交回复
热议问题