在一个类中查找文件,在另一个类中查找输出/进度条

问题描述 投票:0回答:1

我遇到的第一个问题就是达到我的列表大小,当我调用我的类'getResult()时,它在MainWindow中.size()它是零,即使它在filesearch类中正在增加(如果你在搜索期间打印出来) 。

其次,我需要帮助如何让两个摇摆工作者在两个班级之间进行交流。在一个类中它正在寻找文件,而在MainWindow中它正在移动进度条。

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

我尝试在每个类中创建两个swing工作器,一个用于在MainWindow中输出,另一个用于搜索(扩展FileSearch类)

由于我正在寻找递归的文件,我不确定这是否是正确的方法

  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();
}

和MainWindow

    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();

}
java swing swingworker
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.