populate a 'JTable' with values from a '.txt' file

我怕爱的太早我们不能终老 提交于 2019-12-02 09:22:00

you need to change your to something like this.you need to reset vector data = new Vector(); each time when you read new line otherwise it contain first row + second row + so on.and also you can call dtm.setRowCount(0); to avoid empty initial rows . and you need only to add rows the problem of your comment[cell contain lot of columns] is because of dtm.addRow(new Object[]{columns, data}) use dtm.addRow(data); instead and problem will be fixed

code

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

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

    }

output>>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!