简体   繁体   English

如何使用SwingWorker避免冻结GUI

[英]How to use SwingWorker to avoid freeze GUI

I'm doing a program that creates some Excel files (.xlsx) in an specific folder and I've got a problem with GUI because it's freeze when I call to the method that creates this files. 我正在做一个在特定文件夹中创建一些Excel文件(.xlsx)的程序,但是GUI出现问题,因为在调用创建该文件的方法时,它冻结了。

I want that in the GUI there was a progress bar indeterminate that doesn't stot except there was an exception and a JLabel to show the excel file that it's in creation. 我希望在GUI中有一个不确定的进度条,除非有异常和JLabel用来显示创建中的excel文件,否则它不会出错。

MailPanel class: it's the interface, when I click in a button called "Generate" calls to the method "generateExcel" of the class GenerateExcel() MailPanel类:它是接口,当我单击名为“ Generate”的按钮时,调用类GenerateExcel()的方法“ generateExcel”

public class MainPanel extends JPanel {
        .....
        .....
        btnGenerate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                jsonFileIssues = ..... ;
                jsonFileProject = ..... ;
                Project project = ..... ;
                key = project.getKey();
                category = project.getCategory();

                String ruta = "C:\\JIRA\\JIRA-Proyectos\\" + category + "\\";   

                .....
                GenerateExcelExterno gee = new GenerateExcelExterno(jsonFileProject, jsonFileIssues, key, ruta, excelFile);
                gee.execute();
                .....
            }
        }
        .....
        .....
    }

GenerateExcel class: where it's the importante method to generate the files GenerateExcel类:生成文件的重要方法

I know that I've to add SwingWorker to the GenerateExcel class to unfreeze the GUI, but I can't get it! 我知道我必须将SwingWorker添加到GenerateExcel类中才能解冻GUI,但是我无法获得它!

The method CREATES the documents, so the doInBackground works, but the progress bar in the panel continues freezed... 该方法创建文档,因此doInBackground可以工作,但是面板中的进度条继续冻结...

public class GenerateExcel extends SwingWorker<Integer, Void> {

    String jsonFileProject, jsonFileIssues, key, ruta, excelFile;

    public GenerateExcel(String jsonFileProject, String jsonFileIssues,
            String key, String ruta, String excelFile) {
        super();
        this.jsonFileProject = jsonFileProject;
        this.jsonFileIssues = jsonFileIssues;
        this.key = key;
        this.ruta = ruta;
        this.excelFile = excelFile;
    }

    public static void generateExcel(String jsonFileProject, String jsonFileIssues, String key, String ruta, String excelFile) {
        .....
        .....
        .....
    }

    @Override
    protected Integer doInBackground() throws Exception {
        // TODO Auto-generated method stub
        generateExcelExterno(jsonFileProject, jsonFileIssues, key, ruta, excelFile);
        return 100; //for example
    }
}

I think that our ability to answer in detail is hampered by the limited information provided, but the gist of your problem is simply that you need to set up your SwingWorker in your control class -- the ActionListener, and then execute it. 我认为我们提供详细信息的能力受到所提供的有限信息的阻碍,但是问题的要点仅仅是,您需要在控件类ActionListener中设置SwingWorker,然后执行它。 Inside of the SwingWorker's doInBackground() method should go your long-running code call. 在SwingWorker的doInBackground()方法内部应该进行长时间运行的代码调用。 Don't make any Swing calls from within doInBackground. 不要在doInBackground内部进行任何Swing调用。

Some useful links: 一些有用的链接:

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

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