/* Written by Jen Chen on 01/31/05 Note: When we create a JTable, we need to pass the column header (as a one dimensional array) to the setDataVector(cell_Elements[][], columnNames[]) method. Therefore we need to create or pass the desired data to a one-dimensional array, then later we'll pass this array as the second parameter to the method setDataVector in the DefaultTableModel class. The 2nd thing that we have to do is to create a 2-dimensional array to store the data in each cell. Since a cell in a JTable is 2-dimensional, then we need to create a 2-dimensional array to store the cells' data. (row X column). After this 2-dimensional array is created, we'll pass this array to the method setDataVector(cell_Element[][], columnName[]) as the 1st parameter. once this is done then we are ready to create a new table by table = new MyTable();//MyTable is a class extends to JTable. table.setModel(dm); scrollpane = new JScrollPane(table); That is all it takes to create a JTable. */ import javax.swing.table.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.io.*; import java.util.*; public class TableTest{ protected JFrame frame; protected JScrollPane scrollpane; //To make the pane scrollable. We'll insert the JTable inside this pane. protected MyTable table; String inputFile; String outputFile; public TableTest() throws IOException{ // Create the table model. DefaultTableModel dm = new DefaultTableModel(); String file = null, line; String[][] data = new String[100][10]; //A 2-dimensional array to store cells' data. String[] tmp = new String[10]; String[] columnNames = new String[10]; //A 1-dimensional array to store column headers. BufferedReader br = null; File out = new File("filterProducts.csv"); BufferedWriter bw = new BufferedWriter(new FileWriter(out)); try{ br = new BufferedReader(new FileReader("Products.csv")); line = br.readLine(); //read the column headers //Set the column headers by calling the split() method in the String class. //This method will return an array and will be stored into the array columNames[]. columnNames = line.split(","); line = br.readLine(); //start reading into the records. //Create data for the table. int row = 0; //Count the number of rows. while(line != null){ tmp = line.split(","); //Convert the contents of each line(record) into an array of 10-elements. //Start storing data to table's cells. for (int col = 0; col < columnNames.length; ++col){ data[row][col] = tmp[col]; } row++; line = br.readLine(); //Read into the next line (record). }//end of WHILE-statement. } //END of TRY-statement. catch(Exception e) { System.out.println(e); } // Configure the model with the data and column headers. dm.setDataVector(data, columnNames); // Create the table. table = new MyTable(); // Connect the model to the table. table.setModel(dm); // Create a scroll pane for the table. scrollpane = new JScrollPane(table); // Make the table visible. frame = new JFrame(); frame.getContentPane().add(scrollpane); frame.setSize(750, 350); frame.setVisible(true); } public void setFile(String fileName){ this.inputFile = fileName; } public String getFile(){ return this.inputFile; } public void setOutputFile(String outfileName){ this.outputFile = outfileName; } public String getOutputFile(){ return this.outputFile; } public static void main(String[] args) throws IOException{ TableTest test = new TableTest(); } }