How to use Jprogressbar with audio readings in excel

Asked

Viewed 57 times

0

I have a method that receives an Excel file, and a Jtable. This method reads the file and plays the data contained in it within jTable.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class ControleImportPlanilha {

public void importarPlanilha(File arquivo, JTable tabela){

    try {
        //Recebe o arquivo da planilha 
        XSSFWorkbook book = (XSSFWorkbook) WorkbookFactory.create(new FileInputStream(arquivo));
        //Pega a primeira folha da planilha
        Sheet folha = book.getSheetAt(0);
        //percorre as linhas da planilha
        Iterator linhaItetator = folha.rowIterator();
        //prepara o indice das linha para percorrer as celulas
        int indiceLinha = -1;

        DefaultTableModel modeloTable = new DefaultTableModel();

        tabela.setModel(modeloTable);


        while(linhaItetator.hasNext()){

            indiceLinha++;

            Row linha = (Row) linhaItetator.next();

            Iterator colunaIterator = linha.cellIterator();

            int qtdColunas = linha.getLastCellNum();

            Object[] listaColuna = new Object[qtdColunas];

            int indiceColuna = -1;

            while(colunaIterator.hasNext()){

                indiceColuna++;

                Cell celula = (Cell) colunaIterator.next();

                if(indiceLinha == 0){
                    modeloTable.addColumn(celula.getStringCellValue());
                }else{
                    if(celula != null){

                        switch(celula.getCellType()){

                            case Cell.CELL_TYPE_NUMERIC:
                                listaColuna[indiceColuna]=(int)Math.round(celula.getNumericCellValue());
                              //  System.out.print(listaColuna[indiceColuna]=(int)Math.round(celula.getNumericCellValue()));
                                break;

                            case Cell.CELL_TYPE_STRING:
                                listaColuna[indiceColuna]=celula.getStringCellValue();
                             //   System.out.print(listaColuna[indiceColuna]=celula.getStringCellValue());
                                break;

                            case Cell.CELL_TYPE_BOOLEAN:
                                listaColuna[indiceColuna]=celula.getBooleanCellValue();
                           //     System.out.println(listaColuna[indiceColuna]=celula.getBooleanCellValue());
                                break;

                            case Cell.CELL_TYPE_FORMULA:
                                listaColuna[indiceColuna]=celula.getCellFormula();
                              //  System.out.println(listaColuna[indiceColuna]=celula.getCellFormula());
                                break;

                            case Cell.CELL_TYPE_BLANK:    
                                listaColuna[indiceColuna]=celula.getStringCellValue();
                             //   System.out.print(listaColuna[indiceColuna]=celula.getStringCellValue());
                                break;

                            case Cell.CELL_TYPE_ERROR:
                                listaColuna[indiceColuna]=celula.getErrorCellValue();
                             //   System.out.println(listaColuna[indiceColuna]=celula.getErrorCellValue());

                            default:
                                listaColuna[indiceColuna]=celula.getDateCellValue();
                            //    System.out.print(listaColuna[indiceColuna]=celula.getDateCellValue());
                                break;
                        }
                    }
                }

            }
            if(indiceLinha !=0){
             modeloTable.addRow(listaColuna);
            }
            System.out.println("\n");
        }


    } catch (FileNotFoundException ex) {
        Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidFormatException ex) {
        Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
    } catch (EncryptedDocumentException ex) {
        Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
    }

}

}

Depending on the size of the Excel file, the process may take a while and the idea was to add a Jprogressbar, so the user does not have the feeling that the software is stuck.

I call this method from a Jbutton that captures the path of the selected file and calls the method to do the reading

jButtonSelecionarArquivoActionPerformed(java.awt.event.ActionEvent evt) {                                                         
   ControleSelecaoArquivoExcel select = new ControleSelecaoArquivoExcel();

   File[] caminho = select.seleciona();

   ControleImportPlanilha importar = new ControleImportPlanilha();

   importar.importarPlanilha(caminho[0], jTableResultadoPlanilha);

}

Someone can help me do such a feat?

  • 2

    Take a look at Swingworker, what you need is to "parallelize" another Thread, because the swing runs on top of the Event Dispatch Event, and the swingworker is the closest to "multithreading you’ll encounter with the EDT.

  • Correction: Swing runs over the Event Dispatch Thread(EDT). Recommended reading: http://www.devmedia.com.br/trabalhoando-swingworker-em-java/29331

  • Okay I’ll take a look... I had no knowledge of EDT. Thanks

  • Valdecir, this question here (asked by me) helped me to clarify a lot, from a read also.http://answall.com/questions/115168/o-que-%C3%A9-Event-dispatching-thread-edt-em-interfaces-gr%C3%A1ficas Any question of swingworker, just ask on the site. ;)

  • Opa gave a look... Very cool, I will try to implement in this my code to see in practice right. I think only so I will have a better understanding. Thanks for the help.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.