Identify empty columns of a jTable and paint the entire row red

Asked

Viewed 310 times

0

I am importing a file in Excel and playing inside a jTable, so far so good. What happens is that there is user inserting spreadsheet with empty fields. I would like to leave the whole Lina in red and request that it be changed.

This is the code I use to import the spreadsheet to jTable

    DefaultTableModel modeloTable = new DefaultTableModel();
   // ModeloTabela modeloTable = new ModeloTabela(null, col)
    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;

        tabela.setModel(modeloTable);

        while (linhaItetator.hasNext()) {
            indiceLinha++;

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

            Iterator colunaIterator = linha.cellIterator();

            int qtdColunas = linha.getLastCellNum();

            if (qtdColunas == -1) {
                break;
            }

            Object[] listaColuna = new Object[qtdColunas];

            int indiceColuna = -1;
            while (colunaIterator.hasNext()) {

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

                if (indiceLinha == 0) {
                    //ser for sem descrição a coluna, não vai ser criada
                    if (celula.getStringCellValue().equals("")) {
                        break;
                    }
                    modeloTable.addColumn(celula.getStringCellValue());
                } else {
                    if (celula != null) {

                        switch (celula.getCellType()) {

                            case Cell.CELL_TYPE_NUMERIC:
                                if (tipoImportacao == 0) {
                                    if (celula.getColumnIndex() == 3) {

                                        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                                        listaColuna[indiceColuna] = df.format(celula.getDateCellValue());
                                        break;
                                    } else if (celula.getColumnIndex() == 4) {
                                        XSSFCellStyle celStyle = book.createCellStyle();
                                        XSSFDataFormat dataFor = book.createDataFormat();
                                        celStyle.setDataFormat(dataFor.getFormat("#,##0.00"));

                                        celula.setCellStyle(celStyle);

                                        listaColuna[indiceColuna] = celula.getNumericCellValue();
                                        break;
                                    }

                                    listaColuna[indiceColuna] = (int) celula.getNumericCellValue();
                                 //   System.out.println(celula.getNumericCellValue());
                                    break;
                                } else if (tipoImportacao == 1) {
                                    listaColuna[indiceColuna] = (int) celula.getNumericCellValue();
                                //    System.out.println(celula.getNumericCellValue());
                                    break;
                                }
                                listaColuna[indiceColuna] = (int) celula.getNumericCellValue();
                                break;

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

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

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

                            case Cell.CELL_TYPE_BLANK:
                                //caso seja uma linha em branco não sera apresentada na tabela  

                                if (indiceColuna == 0) {
                                    flagVazio = 1;
                                }

                                break;

                            case Cell.CELL_TYPE_ERROR:
                                break;
                            default:

                                break;
                        }
                    }
                }

            }//fim do iterator coluna

            if (flagVazio == 1) {
                indiceLinha--;
                flagVazio = 0;

            } else if (indiceLinha != 0) {
                modeloTable.addRow(listaColuna);
            }
        }//fim do iterator linha

As you can see in this code I am using the Defaulttablemodel because I could not filter the "case" for date, value among other things.

The kind of spreadsheet I get is

Planilha que é lida pelo código

As the table is editable, to avoid the user coming back in Excel, type what is missing and then try again I would like to leave the lines with error in red.

  • 1

    Looking more closely at your code, you should not treat this in the table, but rather, before inserting it into it, as you are doing the case. It may be the case to interrupt the rest of the insertion and inform the user that the spreadsheet contains blank lines and does not complete the import.

  • 1

    If you prefer to do this after the import, leaving the fields highlighted in red (which can be ignored by the user), that is with Cellrenderer, but for that would be necessary a [mcve] of its Jtable and its Tablemodel.

  • 1

    What I had in mind is that until all the red lines were corrected, the next step could not be taken. I will improve the question and specify that.

  • I think this would make the application too complex, if you know how to work with threads, good luck, is a good alternative. If not, it might be better to check this before you populate the table, and inform the user that there are empty cells and not complete the operation.

No answers

Browser other questions tagged

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