从向量填充JTable时出错 [英] Error When Populating JTable from Vectors

查看:98
本文介绍了从向量填充JTable时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用向量从文本文件填充表格.我应该为每一行创建一个新的向量吗?我的代码还有其他问题吗?我不太确定该怎么办.

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

推荐答案

您可以使用

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

此外,至少将e.printStackTrace()(因为您没有使用任何日志记录框架)放入catch块中,以查看发生错误时的堆栈跟踪情况.

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.

这篇关于从向量填充JTable时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆