简体   繁体   English

为什么我的SwingWorker不工作?

[英]Why isn't my SwingWorker working?

My SwingWorker doesn't seem to work. 我的SwingWorker似乎不起作用。 It outputs nothing into the textpanel and nothing into the console. 它什么也不输出到文本面板,什么也不输出到控制台。 It doesn't even execute the actual cmd command in the ProcessBuilder.. 它甚至不执行ProcessBuilder中的实际cmd命令。

I have no clue why it won't work. 我不知道为什么它不起作用。 Anyone seeing anything wrong in the code? 有人在代码中看到任何错误吗?

public class cmdExec extends SwingWorker<Integer, String> {

    private int status;

    private FormPanel formPanel = new FormPanel();
    private FooterBar footerBar = new FooterBar();
    private TextPanel textPanel = new TextPanel();

    public cmdExec() {
        textPanel.appendText((this.getState()).toString());
    }

    @Override
    protected Integer doInBackground() {
        try {

            ProcessBuilder pb = new ProcessBuilder(
                "cmd.exe", "/c", "copy NUL createThis.txt"
            );
            pb.directory(new File(formPanel.workspaceDir.toString()));
            pb.redirectErrorStream(true);
            Process p = pb.start();
            String s;
            BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));

            while((s = stdout.readLine()) != null && !isCancelled()) {
                publish(s);
            }
            if(!isCancelled()) {
                status = p.waitFor();
            }
            p.getInputStream().close();
            p.getOutputStream().close();
            p.getErrorStream().close();
            p.destroy();

        } catch(IOException | InterruptedException ex) {
            ex.printStackTrace(System.err);
        }

        return status;
    }

    @Override
    protected void process(List<String> messages) {
        formPanel.okBtn.setText((this.getState()).toString());
        for(String message : messages) {
            textPanel.appendText(message + "\n");
        }
    }

    @Override
    protected void done() {
        textPanel.appendText((this.getState()).toString() + " " + status);
        formPanel.okBtn.setEnabled(true);
        footerBar.progress.setIndeterminate(false);
        formPanel.disableList(false);
        formPanel.disableWorkspace(false);
    }

}

In my main class I just call a cmdExec.execute(); 在我的主类中,我只是调用cmdExec.execute(); for it to run, and I checked if it even can call it and it does. 使其运行,我检查了它是否可以调用它并且确实可以。 I checked it by adding a sysout to all the methods in my swingworker, and it did print them on the methods it's supposed to run. 我通过将一个sysout添加到我的swingworker中的所有方法中进行了检查,并确实将它们打印在应该运行的方法上。

Have I forgot something? 我忘了什么吗? I'm just getting blind by looking all over it again to try to see what I've missed. 我只是通过再次查看所有内容以尝试查看我错过的内容而变得盲目。

Here's the code calling the SwingWorker class: 这是调用SwingWorker类的代码:

cmdExec cmdExec = new cmdExec();
cmdExec.execute();

This code is in a ActionListener on when a button is pressed. 按下按钮时,此代码位于ActionListener中。

After further research; 经过进一步研究; I noticed that it's because of these 3 lines: 我注意到这是由于以下三行:

private FormPanel formPanel = new FormPanel();
private FooterBar footerBar = new FooterBar();
private TextPanel textPanel = new TextPanel();

But I really need to reach variables inside those classes. 但我确实需要在这些类中访问变量。 How can I do this and have it all working? 我该怎么做并使它们全部正常工作?

New Constructor 新建设者

public cmdExec(FormPanel formPanel,FooterBar footerBar,TextPanel textPanel ) {
            textPanel.appendText((this.getState()).toString());
        }

and in runWorker() you pass the values that you want to be changed. 并在runWorker()中传递要更改的值。

   cmdExec = new cmdExec(formPanel,footerPanel,textPanel);
   cmdExec.execute();

But as see in your code you need to add an PropertyChangeListeners see more in the documentation how to add a progress bar Documentation of SwinWorker 但是,如您的代码所示,您需要添加一个PropertyChangeListeners ,在文档中可以看到更多有关如何添加进度条的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM