populate a 'JTable' with values from a text file

1.1k views Asked by At

I have a text file like this

0786160384|P. K.|Tharindu|912921549v|Colombo|
0711495765|P. K.|Gamini|657414589v|Colombo|
0114756199|H. P.|Weerasigha|657895478v|Kandy|

each |separates a data. I want to display the data on this file in a jTable. below is my code so far.

public class PrintGUI extends javax.swing.JFrame {

File file, tempFile;
FileReader fileReader;
FileWriter fileWriter;
ArrayList ClientList;
Vector data = new Vector();
Vector columns = new Vector();

public PrintGUI() {
    initComponents();
    this.setIconImage(new ImageIcon(getClass().getResource("/Images/icon.png")).getImage());
    file = new File("C:\\Users\\Tharindu\\Documents\\NetBeansProjects\\Geo phonebook\\phonebook.txt");
    tempFile = new File("C:\\Users\\Tharindu\\Documents\\NetBeansProjects\\Geo phonebook\\tempphonebook.txt");
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    PhoneBookTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            "Phone No", "First Name", "Last Name", "NIC", "City"
        }
    ) 
}                                                    

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    String line = null;
    DefaultTableModel dtm = (DefaultTableModel) PhoneBookTable.getModel();

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        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();
        dtm.addRow(new Object[]{columns, data});
    } catch (Exception e) {
        e.printStackTrace();
    }

}

problem is when I run the program it shows a whole line on text from the .txt file instead of a single data per column. more specifically it shows the whole line of

 [0786160384, P. K., Tharindu, 912921549v, Colombo]

in the first row first column and

[0711495765, P. K., Gamini, 657414589v, Colombo, 0114756199, H. P., Weerasigha, 657895478v, Kandy]

in the first row second column

Instead I want it to display single data per cell. can some one please help?

2

There are 2 answers

0
P. K. Tharindu On

Finally found the error. It should be like

try {
        BufferedReader br = new BufferedReader(new FileReader(file));

        while ((line = br.readLine()) != null) {
            data = new Vector();// this is important
            StringTokenizer st1 = new StringTokenizer(line, "|");
            while (st1.hasMoreTokens()) {
                String nextToken = st1.nextToken();
                data.add(nextToken);
                System.out.println(nextToken);

            }
            System.out.println(data);
            dtm.addRow(data);//add here 
            System.out.println(".................................");
        }

        br.close();

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

}
2
Robin On

You only call addRow once, so of course there will be only one row added to the table model. You need to move that statement inside your file-reading loop.

Each line you read from the file corresponds to a row in your table. As such, you need to call addRow each time you have read a line, and call it with the contents of that line.