1

I recently started using OpenCSV's CSVReader to get data from a CSV file to a JTable in java, but I keep getting an error. DaTroop gave the answer to how to get data from the CSV here: import csv to JTable . In my case I copied the code

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

into Netbeans IDE 7.4, but I keep getting the error "Incompatible types - Object Cannot be converted to TableModel". in the

(myEntries.toArray());

Any ideas?

Thanks

As in comment, please find my whole code snippet. The JFrame works and views, so it should be called fine.

public static void LoadLog() throws FileNotFoundException, IOException {
        LogViewer log = new LogViewer();
        log.setVisible(true);
CSVReader reader = new CSVReader(new FileReader("testog.csv")); 

String[][] rowData = {
    { "A", "B" },
    { "C", "D" }
};

List<String[]> myEntries = reader.readAll();
rowData = myEntries.toArray(new String[0][]);

String[] columnNames = { "Column 1", "Column 2" };

System.out.println(myEntries);

table1 = new JTable(rowData, columnNames);

log.pack();
log.setVisible(true);
    }
Community
  • 1
  • 1
anitag95
  • 307
  • 2
  • 16

1 Answers1

2

First, note that reader.readAll() returns List<String[]>.

Also note that calling myEntries.toArray() returns Object[], and not Object[][], or more specifically String[][], which would be more preferable to work with.

The closest constructor for JTable that matches what you're trying to do would be this one:

JTable(Object[][] rowData, Object[] columnNames) 

So you need to supply both rowData and columnNames. You can get the rowData like this:

List<String[]> myEntries = reader.readAll();
String[][] rowData = myEntries.toArray(new String[0][]);

Create a variable to hold the column names (or don't, your choice, but column names are still required):

String[] columnNames = { "Column 1", "Column 2" };

And then create your JTable:

JTable table = new JTable(rowData, columnNames);

Based on your comment, here's a short and simple code snippet for creating and displaying a table. As long as you initialize rowData and columnNames with the correct data, then it should work fine.

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String[][] rowData = {
    { "A", "B" },
    { "C", "D" }
};
String[] columnNames = { "Column 1", "Column 2" };
JTable table = new JTable(rowData, columnNames);
frame.add(new JScrollPane(table));

frame.pack();
frame.setVisible(true);
sgbj
  • 2,264
  • 17
  • 14
  • Is there any further code to add to construct the table? This is my final code and when using System.out.pritln, I am getting data out, but I am not seeing any data in my table? – anitag95 Jul 14 '14 at 17:54
  • I've added an example of how to create and show a table. If you can't get your program to work after making any necessary modifications, then you may need to show us more of your code. – sgbj Jul 14 '14 at 18:45
  • Where in your code do you add `table1` to your `log`/JFrame? – sgbj Jul 15 '14 at 14:36
  • It is added using a Pallete in Netbeans. The table variable is renamed using the properties. Could you maybe provide a way to add it to the frame without adding it using the palette adder in Netbeans? – anitag95 Jul 16 '14 at 15:23
  • Using your code I realised that I'm not setting a model table. Thanks for your help! – anitag95 Jul 17 '14 at 06:37