0

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.

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.

MailPanel class: it's the interface, when I click in a button called "Generate" calls to the method "generateExcel" of the class 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

I know that I've to add SwingWorker to the GenerateExcel class to unfreeze the GUI, but I can't get it!

The method CREATES the documents, so the doInBackground works, but the progress bar in the panel continues freezed...

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
    }
}
  • `"...but I can't get it!"` -- then you'll want to help us help you. You'll want to create and post a [minimal example program](http://stackoverflow.com/help/mcve) where you create a very short program that tries to mimic your problem. This short program compiles and runs, has no code that access Excel but rather substitutes a `Thread.sleep(...)` for a long-running process. Let's see your best attempt to create and use a SwingWorker, and then we'll get a much better idea of what you could be doing wrong. – Hovercraft Full Of Eels Sep 24 '14 at 20:57
  • You'll also want to describe in detail how your attempt is not working. – Hovercraft Full Of Eels Sep 24 '14 at 20:59
  • Looks like a good start...what's the problem – MadProgrammer Sep 24 '14 at 21:18
  • `"The method doesn't create the files and the gui continues freeze..."` -- then you're not calling the long-running code in the SwingWorker's `doInBackground()` method. Listen, we can fart around with guessing from here til forever, but if you really want a decent answer, your smart move will be to create and post some sort of [MCVE](http://stackoverflow.com/help/mcve). Your choice. – Hovercraft Full Of Eels Sep 24 '14 at 21:22
  • The code is more difficult, thats a summary. I just realize that the files are created in background but the progress bar I want not to stop is freezed... – Mario Romero Remírez Sep 24 '14 at 21:32
  • ...and the code to manipulate the JProgressBar is where? I fear this is going to become like a slow dental extraction without anesthesia where we slowly painfully pry information from you. Let's not go this route. – Hovercraft Full Of Eels Sep 24 '14 at 21:36
  • It's an indeterminate progress bar, I thought that I have to insert it in the panel and not to manipulate it more. I want that it doesn't stop moving... – Mario Romero Remírez Sep 24 '14 at 21:39
  • I'm sorry, but I don't see a way for us to guess why code not shown isn't working. If you can isolate your problem, you're over half way there to solving it. Keep at it. – Hovercraft Full Of Eels Sep 24 '14 at 21:48

1 Answers1

1

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. Inside of the SwingWorker's doInBackground() method should go your long-running code call. Don't make any Swing calls from within doInBackground.

Some useful links:

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373