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?
Finally found the error. It should be like