0

I have a problem with my progressbar. It does not update, in fact my whole jframe freezes when it is busy executing code. I have managed to set the correct value to the progressbar.

My code creates flyers, what works on its own and it does that perfectly. Just my JFrame itself freezes while doing this, this also creates it that i cannot use any buttons. Why is my JFrame freezing while executing code?

Start jframe class:

if (excelFileLocation.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "U heeft geen excel bestand gekozen, kies eerst een excel bestand.", "Geen excel bestand gekozen.", JOptionPane.ERROR_MESSAGE);
        } else if (folderLocation.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "U heeft geen eindmap gekozen voor de gemaakte flyers, kies eerst een eindmap.", "Geen map gekozen.", JOptionPane.ERROR_MESSAGE);
        } else if (logoFolderLocation.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "U heeft map gekozen met de schoollogo's, kies eerst een map.", "Geen map gekozen.", JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                makeFlyers.setBackground(Color.red);
                makeFlyers.setEnabled(false);
                JOptionPane.showMessageDialog(null, "", "Flyers worden gemaakt!", JOptionPane.INFORMATION_MESSAGE);
                Excel excel = new Excel();
                excel.readExcel(new File(excelFileLocation.getText()), new File(this.folderLocation.getText()), new File(logoFolderLocation.getText()), progressBar);
                System.out.println("Klaar met flyers maken!");
                JOptionPane.showMessageDialog(null, "Alle flyers zijn succesvol gemaakt!", "Succes!", JOptionPane.PLAIN_MESSAGE);
                resetButton(makeFlyers);
            } catch (IIOException ex) {
                JOptionPane.showMessageDialog(null, "Het logo kan niet worden geplaatst, kijk of er correct word verwezen naar de logo.", "Fout bij plaatsen van logo", JOptionPane.ERROR_MESSAGE);
                handleException(ex);
                resetButton(makeFlyers);
            } catch (InvalidFormatException | ParseException ex) {
                handleException(ex);
                resetButton(makeFlyers);
            } catch (IndexOutOfBoundsException | NullPointerException ex) {
                System.out.println("Klaar met flyers maken!");
                JOptionPane.showMessageDialog(null, "Alle flyers zijn succesvol gemaakt!", "Succes!", JOptionPane.PLAIN_MESSAGE);
                resetButton(makeFlyers);
            } catch (IOException ex) {
                handleException(ex);
                JOptionPane.showMessageDialog(null, "Het excel bestand wat u wilt gebruiken kan niet worden gevonden, of staat nog open in een ander venster.", "Fout bij uitlezen van Excel bestand", JOptionPane.ERROR_MESSAGE);
                resetButton(makeFlyers);
            }
        }

This is my Excel functions:

public class Excel {

    JProgressBar progressBar;
    File outputFolder;
    File logoFolder;

    public void readExcel(File excelFile, File output, File logoLocation, JProgressBar pb) throws IOException, InvalidFormatException, ParseException {
        this.outputFolder = output;
        this.logoFolder = logoLocation;
        progressBar = pb;
        // Retrieving the number of sheets in the Workbook
        try ( // Creating a Workbook from an Excel file (.xls or .xlsx)
                Workbook workbook = WorkbookFactory.create(excelFile)) {
            for (Sheet sheet : workbook) {
                if (sheet.getSheetName().equals("secundair flyer")) {
                    createFlyersFromExcel(sheet);
                }
            }
        }
    }

    public void handleException(Exception ex) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        ex.printStackTrace(pw);
        System.out.println(sw.toString());
    }

    public void createFlyersFromExcel(Sheet sheet) throws IOException, ParseException {
        DataFormatter dataFormatter = new DataFormatter();
        boolean firstRow = false;
        int currentRow = 0;
        progressBar.setMaximum(sheet.getPhysicalNumberOfRows());
        System.out.println(progressBar.getMaximum());
        for (Row row : sheet) {
            currentRow++;
            if (firstRow == false) {
                firstRow = true;
            } else {
                if (!dataFormatter.formatCellValue(row.getCell(0)).equals("0")) {
                    List<String> values = new ArrayList<>();
                    for (Cell cell : row) {
                        cell.setCellFormula(null);
                        String cellValue = dataFormatter.formatCellValue(cell);
                        if (!cellValue.equals("0")) {
                            values.add(cellValue);
                        } else {
                            values.add(null);
                        }
                    }
                    if (values.get(1) == null) {
                    } else {
                        School s = new School(values, logoFolder);
                        Flyer flyer = new Flyer(s, outputFolder);
                    }
                }

            }
            progressBar.setValue(currentRow);
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • It's because everything is done in the same thread and no repaint is made before you reach the end of your process. Mabybe this could help you : https://stackoverflow.com/questions/48144967/how-to-asynchronous-trigger-jprogressbar – Olivier Depriester May 12 '20 at 09:22
  • @OlivierDepriester I do not get it.. i've tried working it out but it just doesnt work – PRIVATE ACCOUNT May 18 '20 at 08:29
  • Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson May 18 '20 at 11:19
  • @AndrewThompson how am i blocking the EDT? Ive read the whole thing but I just do not get what i am doing wrong.. – PRIVATE ACCOUNT May 18 '20 at 12:05

0 Answers0