-3

I want to import a CSV file into a JTable using the OpenCSV library. All the examples found on the net are related to command line display, the only code found here does not work because JTable needs Object[][] and a String[]:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv")); 
List myEntries = reader.readAll();
JTable table = new JTable(myEntries.toArray());

Source: import csv to JTable

Is there any way to display these files in a table with this library?

Bishop
  • 1
  • 5
  • Why do you quote the code of that answer and not of [this one](https://stackoverflow.com/a/35471186/16376827)? – g00se Sep 14 '22 at 15:51
  • @g00se Because the line "transient CSVReader CSVFileReader;" return the error message "Illegal modifier for the variable CSVFileReader; only final is permitted". – Bishop Sep 14 '22 at 16:04
  • (1-) What line? There is no statement with the keyword "transient" in the posted code. – camickr Sep 14 '22 at 16:15
  • That's not *actual* code. Note the *something like* of the comment. The solution is to fix *that* error, not to choose an answer that didn't work. Remove ```transient``` and put that code in its own method (obvs. separate from the ```import``` statements) – g00se Sep 14 '22 at 16:17
  • @camickr It's featured in this post suggested by g00se: https://stackoverflow.com/questions/10466079/import-csv-to-jtable/35471186#35471186 – Bishop Sep 14 '22 at 16:40
  • @g00se: You are right, in fact I wrote a second post with the code working (at least for me). – Bishop Sep 14 '22 at 16:41
  • *It's featured in this post* - we are not interested in that post. We are interested in the code posted with your question. We can't go chasing links all over the net to guess what you are actually testing. – camickr Sep 14 '22 at 20:20

1 Answers1

0

I solved it with this code

import com.opencsv.*;

private Object[] dataColumn;
private DefaultTableModel tableModel = new DefaultTableModel();

try{
    tableModel = (DefaultTableModel) JTable.getModel();

            CSVReader reader = new CSVReaderBuilder(new FileReader(CSV_file))
                    .withCSVParser(new CSVParserBuilder()
                            .withQuoteChar('\'')
                            .withSeparator('\t')
                            .build())
                    .build();
            List myEntries = reader.readAll();
            dataColumn = (String[]) myEntries.get(0);
            tableModel = new DefaultTableModel(dataColumn, myEntries.size()-1); 
            int rowcount = tableModel.getRowCount();
            for (int x = 0; x<rowcount+1; x++){
                int columnnumber = 0;
                // if x = 0 this is the first row...skip it... data used for columnnames
                if (x>0){
                    for (String thiscellvalue : (String[])myEntries.get(x)){
                        tableModel.setValueAt(thiscellvalue, x-1, columnnumber);
                        columnnumber++;
                    }
                }
            }
            reader.close();
} catch (Exception ex) {
    ex.printStackTrace();
}
JTable.setModel(tableModel);
Bishop
  • 1
  • 5
  • 2
    Answers are expected to have some explanation of both the problem and the solution. This site is meant to be more than a library of code snippets. – Basil Bourque Sep 14 '22 at 16:50