Java 从向量填充JTable时出错

Java 从向量填充JTable时出错,java,swing,vector,jtable,Java,Swing,Vector,Jtable,我正在尝试使用向量从文本文件填充表格。我应该为每一行创建一个新的向量吗?我的代码是否还有其他问题?我不太清楚接下来该怎么办 public class myJTable { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Vector<

我正在尝试使用向量从文本文件填充表格。我应该为每一行创建一个新的向量吗?我的代码是否还有其他问题?我不太清楚接下来该怎么办

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);
    }
}
公共类myJTable{
公共静态void main(字符串[]args){
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
向量v=新向量();
向量行数据=新向量();
//字符串拆分
试一试{
FileReader-fReader=newfilereader(“lakedata.txt”);
BufferedReader infle=新的BufferedReader(fReader);
字符串输入;
字符串[]温度;
而((input=infle.readLine())!=null){
温度=输入分割(“,”,3);

对于(int i=0;i而不是使用Vector,您可以创建一个表,然后将其设置为表的模型,然后调用其
addRow
方法直接将读取行结果放入表中。当向表中添加数据时,表将自动更新自身

我冒昧地修改了你的代码,使之更干净

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);
            }
        });
    }
}

另外,至少放置
e.printStackTrace()
(因为您没有使用任何日志框架)在catch块中查看堆栈跟踪,以防捕捉到错误。

是。为每行创建一个新向量。我应该在何处创建新向量?在while循环中?是。就在
for
循环之前。@LeeMeador我似乎无法在注释框中获得正确格式的代码,但我得到一个异常,在将新向量的创建放在for循环之前之后,程序在运行时终止,并且表和向量未填充。