0

I have a csv file (5 columns, with | as delimiter) and I want to fill 2 JTable columns with two data columns from csv file.

public class jTable extends JFrame {

    public static int rowNumber() {
        int num = 0;
        try {
            File files = new File("C:\\BorsaItalia2.csv");
            BufferedReader bf = new BufferedReader(new FileReader(files));
            String lines = "";
            while ((lines = bf.readLine()) != null) {
                num++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }
    JTable table;

    public jTable() {
        setLayout(new FlowLayout());
        String[] columnNames = {"a", "b", "c", "d", "e"};

        Object[][] data = new Object[rowNumber()][5];

        table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(600, 950));
        table.setFillsViewportHeight(true);

        JScrollPane scroll = new JScrollPane(table);
        add(scroll);
    }

    public static void main(String[] args) {
        try {
            int row = 0;
            int col = 0;

            Object[][] imported = new Object[rowNumber()][5];

            File file = new File("C:\\BorsaItalia2.csv");

            BufferedReader bfr = new BufferedReader(new FileReader(file));
            String line = "";

            while ((line = bfr.readLine()) != null) {

                StringTokenizer st = new StringTokenizer(line, "|");

                col = 0;
                while (st.hasMoreTokens()) {
                    imported[row][col] = st.nextToken();

                    System.out.println("number["+ row + "]["
                        + col + "]:" + imported[row][col]);
                    col++;
                }
                row++;
            }
            bfr.close();

            Object[] description = new Object[imported.length];
            Object[] symbol = new Object[imported.length];

            for (int i = 0; i < imported.length; i++) {
                description[i] = imported[i][2];
                symbol[i] = imported[i][0];
            }
            for (int i = 1; i < imported.length; i++) {
                System.out.println("Description is " + description[i]
                    + " and symbol is: " + symbol[i]);
            }
            System.out.println("");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("error " + e);
        }
        jTable gui = new jTable();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(650, 950);
        gui.setVisible(true);
        gui.setTitle("Project Table");
    }
}

I would like to fill table column a with Object[] symbol and column c with Object[] description.

Any coding help really appreciated.

Thanks all.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Alberto acepsut
  • 1,972
  • 10
  • 41
  • 87
  • Please check the code formats as you expect before posting it. I cannot understand that mess! Of course, things like `jTable extends JFrame` don't help. It's a table, it's a frame, no - it's *Superman!* – Andrew Thompson Jan 27 '12 at 16:12

1 Answers1

1

Instead of using the DefaultTableModel implied by your JTable constructor, create your own implementation of AbstractTableModel, as shown in How to Use Tables: Creating a Table Model. Arrange for your getValueAt() to return the data from the arrays you've read. There's a related example here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045