Error When Populating JTable from Vectors

2019-08-13 08:20发布

问题:

I am trying to populate a table from a text file using vectors. Should I be creating a new vector for each row? Is there anything else that appears wrong with my code? I'm not quite sure what to do from here.

public class myJTable {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Vector<String> v = new Vector<String>();
        Vector<Vector<String>> rowData = new Vector<Vector<String>>();

        //String splitting
        try {
            FileReader fReader = new FileReader("lakedata.txt");
            BufferedReader inFile = new BufferedReader(fReader);
            String input;
            String[] temp;

            while((input=inFile.readLine())!=null) {
                temp = input.split(",",3);
                for(int i=0; i<temp.length; i++) {
                    v.addElement(temp[i]);
                    System.out.println(temp[i]);
                    }

                System.out.println(v);
                rowData.addElement(v);
                }

            inFile.close();
            } 

        catch(Exception e) {
                System.out.println("ERROR");    
        }

        Vector<String> columnNames = new Vector<String>();
        columnNames.addElement("Depth");
        columnNames.addElement("Temperature");
        columnNames.addElement("D.O.");
        JTable table = new JTable(rowData, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.setSize(500,300);
        frame.setVisible(true);
    }
}

回答1:

Instead of using Vector, you can create a DefaultTableModel then set it as the model of the table, then invoke its addRow method to directly put the read line result to the table. The table will automatically update itself when data to a table is added.

I took the liberty to revise your code into a cleaner one.

import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.FileReader;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class TablePopulation {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override public void run() {

                JTable table = new JTable();
                JScrollPane scrollPane = new JScrollPane(table);

                /* Set the table data to null first since 
                 * we will fetch data from file line by line
                 * -------------------------------------- */
                DefaultTableModel model = new DefaultTableModel(null, new String[]{"Depth","Temperature","D.O."});
                table.setModel(model);

                /* String splitting
                 * ---------------- */
                try {
                    FileReader fReader = new FileReader("file.txt");
                    BufferedReader inFile = new BufferedReader(fReader);
                    String input = inFile.readLine();

                    while(input !=null) {
                        String[] temp = input.split(",");
                        model.addRow(temp);

                        input = inFile.readLine();
                    }

                    inFile.close();
                }catch(Exception e) {
                    e.printStackTrace();
                }

                JFrame frame = new JFrame();

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(scrollPane, BorderLayout.CENTER);
                frame.setSize(500,300);
                frame.setVisible(true);
            }
        });
    }
}

Also, atleast put e.printStackTrace() (since you're not using any logging framework) in the catch block to see the stack trace of what's going on in case some error has been caught.