简体   繁体   中英

Read file data into JTable

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.

Well I just took your code and compile it and it worked just fine. Just make sure that you are making the correct import and that the input file is in your path. ie("student.txt" should be in the same folder as your java file).

The JavaDoc says this about StringTokenizer

The tokenizer uses the default delimiter set, which is " \\t\\n\\r\\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character.

So make sure that the fields in your input file are using these as you are using the default delimiter.

I used the following imports to make it work

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;


public class InsertFileDataToJTable extends AbstractTableModel {
 // the rest of the code is same as yours

}

One important concern I want to let you know with the code. You have written everything in the table model. Your table model class should only contain the model that is required for the table like column names , column count , row count , column class , getValueAt , setValueAt methods. Please make a separate class for the UI and create a table then create a model and set the model to the table. (If the code is just SSCCE then its good else please separate the UI and the logic.)

The problem with this code is, if you implement AbstractTableModel you have to implement all the required methods for the table like setting column names, setting value, getting values etc. You are not setting column names in this.

If you have a specific reason to use AbstractTableModel then its fine. Else use DefaultTableModel almost all the required methods are implemented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM