I'm writing a small program that creates a gui to display the contents of a csv file. I've tried following the outline from the Oracle website (http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data), but my problem is that the 'getColumnCount' method that is used to build the table can't access the 'headers' variable. Or more likely, it can, but the changes I thought I made to it in the main method did not connect to it. If anyone can shed some light on what's wrong and how to fix it, it'd be much appreciated.
public class MyTableModel implements TableModel {
private String[] headers; //This line.
private Object[][] tableData;
public static void main(String[] args) {
String fileName = "products.csv";
String[] csvList = readCSV(fileName);
String[] headers = Arrays.copyOfRange(csvList, 0, 10); //Or maybe this line isn't changing the one above.
}
private static String[] readCSV(String file) {
//Some code to fill the list.
return fileString;
}
@Override
public int getColumnCount() {
return headers.length; //<<This line of code
}
}
@Hovercraft Full Of Eels
Oh, I should have mentioned. I'm implementing this class like this, which is to say, I'm calling it from elsewhere.
private static void createGUI() {
csvTabler table = new csvTabler();
table.setTitle("CSV Table");
table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table.createJTable();
table.pack();
table.setVisible(true);
}
private void createJTable() {
jTable = new JTable(new MyTableModel());
}
I'm sure this affects your solution but I'm not sure how to adjust..