将文件数据读入JTable [英] Read file data into JTable

查看:208
本文介绍了将文件数据读入JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从.txt文件读取数据并将它们发送到我的表格。
我该怎么办?
我的代码:

$ p $ public class InsertFileDataToJTable extends AbstractTableModel {
Vector data;
向量列;

public InsertFileDataToJTable(){
String line;
data = new Vector();
columns = new Vector();
尝试{
FileInputStream fis = new FileInputStream(student.txt);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringTokenizer st1 = new StringTokenizer(br.readLine(),);
while(st1.hasMoreTokens())
columns.addElement(st1.nextToken()); ((line = br.readLine())!= null){
StringTokenizer st2 = new StringTokenizer(line,);
while(st2.hasMoreTokens())
data.addElement(st2.nextToken());
}
br.close();
} catch(Exception e){
e.printStackTrace();


$ b public int getRowCount(){
return data.size()/ getColumnCount();


public int getColumnCount(){
return columns.size();


public Object getValueAt(int rowIndex,int columnIndex){
return(String)data.elementAt((rowIndex * getColumnCount())
+ columnIndex) ;


public static void main(String s []){
InsertFileDataToJTable model = new InsertFileDataToJTable();
JTable table = new JTable();
table.setModel(model);
JScrollPane scrollpane = new JScrollPane(table);
JPanel面板=新JPanel();
panel.add(scrollpane);
JFrame frame = new JFrame();
frame.add(面板,中心);
frame.pack();
frame.setVisible(true);
}
}

请帮忙!谢谢。

解决方案



我运行你的代码,它工作得很好,除了列名将是默认的A,B,...因为你没有从模型中检索列名的方法。你已经实现了方法getValueAt(),并且完美地工作。在上面的类中为列名添加新的方法:
$ b $ pre $ public String getColumnName(int i){
return(String )columns.get(ⅰ);





$ b如果还有其他问题,我只运行了2列,所以没有完全测试getValueAt()方法。


I want to read data from a .txt file and send them to my table. What should I do? My code:

public class InsertFileDataToJTable extends AbstractTableModel {
    Vector data;
    Vector columns;

    public InsertFileDataToJTable() {
            String line;
            data = new Vector();
            columns = new Vector();
            try {
                    FileInputStream fis = new FileInputStream("student.txt");
                    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                    StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");
                    while (st1.hasMoreTokens())
                            columns.addElement(st1.nextToken());
                    while ((line = br.readLine()) != null) {
                            StringTokenizer st2 = new StringTokenizer(line, " ");
                            while (st2.hasMoreTokens())
                                    data.addElement(st2.nextToken());
                    }
                    br.close();
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }

    public int getRowCount() {
            return data.size() / getColumnCount();
    }

    public int getColumnCount() {
            return columns.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
            return (String) data.elementAt((rowIndex * getColumnCount())
                            + columnIndex);
    }

    public static void main(String s[]) {
            InsertFileDataToJTable model = new InsertFileDataToJTable();
            JTable table = new JTable();
            table.setModel(model);
            JScrollPane scrollpane = new JScrollPane(table);
            JPanel panel = new JPanel();
            panel.add(scrollpane);
            JFrame frame = new JFrame();
            frame.add(panel, "Center");
            frame.pack();
            frame.setVisible(true);
    }
}

please help! Thank you.

解决方案

What problem you are getting in your code?

I run your code, and it worked perfectly, except column names will be default A, B,... as you have not method for retrieving column names from model. You have implemented method getValueAt(), and working perfectly. Just add new method for column names in above class:

public String getColumnName(int i){
    return (String)columns.get(i);
}

If you have any other problem then let use know. I have run using 2 columns only, so not tested getValueAt() method perfectly.

这篇关于将文件数据读入JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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