i have problem reading the data from text file. The first line in my text file should go to 1st row but it go to the column, here is my code.

JScrollPane scrollPane_1 = new JScrollPane();
        scrollPane_1.setBounds(30, 288, 944, 207);
        frmDevelopNewProgram.getContentPane().add(scrollPane_1);
        String[] columns={"Program Code","Program Name" ,"Major","Abbreviation"};
        lap_table = new JTable();
        scrollPane_1.setViewportView(lap_table);
        DefaultTableModel tableModel;
        // specify number of columns
        tableModel = new DefaultTableModel(0,0); 
        tableModel.setColumnIdentifiers(columns);
        lap_table.setModel(tableModel);
        String line;
        //BufferedReader reader;
            try{       
                 FileInputStream fis = new FileInputStream("C:\\Users\\Alex Ting\\workspace\\gui\\src\\Files\\output.txt");
                 BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                StringTokenizer st1 = new StringTokenizer(br.readLine(), "___");

                while (st1.hasMoreTokens())
                    tableModel.addColumn(st1.nextToken());

                while((line = br.readLine()) != null) 
                {
                //tableModel.addColumn(line.split("___"));
                tableModel.addRow(line.split(",")); 
                }
                br.close();
             }
            catch(IOException e){
                JOptionPane.showMessageDialog(null, "Error");
        e.printStackTrace();

            }

You read the first line (17) then use its tokens to add columns (20). You then read the rest of the lines (22) and use those to add rows (25). That's why the first line of the file goes to the column names.

but when i type

tableModel.addRow(st1.nextToken());

it gave my syntax error.

i found out my mistake already, here is code for jpanel with scrollbar and read the data from text file and show it on jtable

JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 21, 987, 368);
        frmListAllUnits.getContentPane().add(scrollPane);
        String[] columns={"Unit Code","Unit Name" ,"Major","Type","Pre-requisite","Co-requisite"};
        table = new JTable();
        scrollPane.setViewportView(table);
        DefaultTableModel tableModel;
        tableModel = new DefaultTableModel(0,6); 
        tableModel.setColumnIdentifiers(columns);
        table.setModel(tableModel);
        String line;

        try{       
            FileInputStream fis = new FileInputStream("C:\\Users\\Alex Ting\\workspace\\gui\\src\\Files\\units.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));

           while((line = br.readLine()) != null) 
           {

           tableModel.addRow(line.split("___")); 
           }
           br.close();
        }
       catch(IOException e){
           JOptionPane.showMessageDialog(null, "Error");
   e.printStackTrace();
       }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.