在一个 class 和另一个 outputing/progressbar 中查找文件

Looking for files in one class and outputing/progressbar in the other

我遇到的第一个问题是在主窗口中达到我的列表大小,当我调用我的 class' getResult().size() 时它为零,即使它在文件搜索 class(如果你在搜索时打印出来)。

其次,我需要帮助如何让两个 swingworkers 在两个 classes 之间进行通信。在一个 class 中它正在寻找文件,在 MainWindow 中它在移动进度条。

我发现我需要使用 SwingPropertyChangeSupport 做一些事情,但我无法实现它。

我尝试在每个 class 中创建两个 swingworker,一个用于在 MainWindow 中输出,另一个用于搜索(扩展 FileSearch class)

而且由于我正在递归地查找文件,所以我不确定这是否是正确的方法

  if (temp.isDirectory()) {
        new FileSearch(getFileNameToSearch(),temp).execute();
}  

这东西^

public class FileSearch extends SwingWorker<Integer, String> {

private String fileNameToSearch;
private List<String> result;
private File file;

public FileSearch(String fileNameToSearch, File file) {
    this.fileNameToSearch = fileNameToSearch;
    this.file = file;
    result = new ArrayList<>();
}

public String getFileNameToSearch() {
    return fileNameToSearch;
}


public List<String> getResult() {
    return result;
}


@Override
protected Integer doInBackground() throws Exception {
    if (file.isDirectory()) {
        System.out.println("Searching directory ... " + file.getAbsoluteFile());

        if (file.canRead()) {
            try{
                for (File temp : file.listFiles()) {
                    if (temp.isDirectory()) {
                        new FileSearch(getFileNameToSearch(),temp).execute();
                    } else {
                        String temp2 = temp.getName().toLowerCase();
                        String trimmedName = temp.getName().toLowerCase();
                        try{
                            trimmedName = temp2.substring(0, temp2.lastIndexOf("."));
                        }catch(StringIndexOutOfBoundsException e){

                        }
                        if (getFileNameToSearch().equals(trimmedName)) {
                            result.add(temp.getAbsoluteFile().toString());
                            System.out.println(result.size());

                        }else{
                            System.out.println(getFileNameToSearch() + "!= " + trimmedName);
                        }

                    }
                }
            }catch(NullPointerException e){

            }


        } else {
            System.out.println(file.getAbsoluteFile() + "Permission Denied");
        }
    }
    return result.size();
}

和主窗口

    public class MainWindow extends JFrame {
    public MainWindow() {
    initComponents();
    fileChooser1.setApproveButtonText("Select");
    fileChooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    directoriesTextArea.setEditable(false);

}

private void startButtonMouseClicked(MouseEvent e) {
    directoriesTextArea.setText("");

    SwingWorker<Void, String> worker =  new SwingWorker<>(){
        @Override
        protected Void doInBackground() throws Exception {
            File file = new File(absolutePathTextField.getText());
            FileSearch filesearch = new FileSearch(fileNameTextField.getText(), file);

            filesearch.execute();

            int count = filesearch.getResult().size();

            if(count == 0){
                JOptionPane.showMessageDialog(new JFrame(), "There are no such files");
                System.out.println(count);
            }else{
                for(String matched : filesearch.getResult()){
                    publish(matched);
                }
            }
            return null;
        }

        @Override
        protected void process(List<String> chunks) {
            for(String matched : chunks){
                directoriesTextArea.append(matched + "\n");
            }
        }

        @Override
        protected void done() {
            directoriesTextArea.append("\nDone!");

        }
    };

    worker.execute();


}

=================编辑版本=================

private void startButtonMouseClicked(MouseEvent e) {
    directoriesTextArea.setText("");
    progressBarCounter = 0;
    progressBar1.setValue(0);


    SwingWorker<Void, String> worker = new SwingWorker<Void, String>() {
        List<String> result = new ArrayList<>();

        @Override
        protected Void doInBackground() throws Exception {
            File file = new File(absolutePathTextField.getText());
            FileSearch fileSearch = new FileSearch(fileNameTextField.getText(), file);

            folderCount(file);
            searchFiles(file);

            return null;
        }

        @Override
        protected void process(List<String> chunks) {
            for(String file : chunks){
                directoriesTextArea.append(file + "\n");
            }
        }
        protected void folderCount(File file){
            if (file.isDirectory()) {

                if (file.canRead()) {
                    try {
                        for (File temp : file.listFiles()) {
                            if (temp.isDirectory()) {
                                folderCount(temp);
                                folderCount++;
                            }
                        }
                    } catch (NullPointerException e) {

                    }
                }
            }
            progressBar1.setMinimum(0);
            progressBar1.setMaximum(folderCount);
        }


        protected void searchFiles(File file) {
        progressBarCounter++;
            if (file.isDirectory()) {
                System.out.println("Searching directory ... " + file.getAbsoluteFile());

                if (file.canRead()) {
                    try {
                        for (File temp : file.listFiles()) {
                            progressBar1.setValue(progressBarCounter);
                            if (temp.isDirectory()) {
                                searchFiles(temp);
                            } else {
                                String temp2 = temp.getName().toLowerCase();
                                String trimmedName = temp.getName().toLowerCase();
                                try {
                                    trimmedName = temp2.substring(0, temp2.lastIndexOf("."));
                                } catch (StringIndexOutOfBoundsException e) {

                                }
                                if (fileNameTextField.getText().toLowerCase().equals(trimmedName)) {
                                    System.out.println(fileNameTextField.getText() + " == " + trimmedName);
                                    publish(temp.getAbsolutePath());

                                } else {
                                    System.out.println(fileNameTextField.getText() + "!= " + trimmedName);
                                }

                            }
                        }
                    } catch (NullPointerException e) {

                    }


                } else {
                    System.out.println(file.getAbsoluteFile() + "Permission Denied");
                }
            }
        }
    };
    worker.execute();

}

您缺少进度/流程组合。在后台线程中,您需要定期调用 process 方法,以说明后台进程的进度。随后,为您调用进度方法(在 EDT 线程内),您可以在那里更新进度条。 https://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html