How To Populate JTable From Txt File Text Using Java NetBeans
In this Java Tutorial we will see How To Import Text From Text File And Set It Into JTable Rows Using BufferedReader + DefaultTableModel On JButton Click In Java programming language and NetBeans IDE .
Project Source Code:
private void jButtonImportActionPerformed(java.awt.event.ActionEvent evt) {
String filePath = "C:\\Users\\Desktop\\table.txt";
File file = new File(filePath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
// get the first line
// get the columns name from the first line
// set columns name to the jtable model
String firstLine = br.readLine().trim();
String[] columnsName = firstLine.split(",");
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
model.setColumnIdentifiers(columnsName);
// get lines from txt file
Object[] tableLines = br.lines().toArray();
// extratct data from lines
// set data to jtable model
for(int i = 0; i < tableLines.length; i++)
{
String line = tableLines[i].toString().trim();
String[] dataRow = line.split("/");
model.addRow(dataRow);
}
} catch (Exception ex) {
Logger.getLogger(TextFileDataToJTable.class.getName()).log(Level.SEVERE, null, ex);
}
}
OutPut:
Download Projects Source Code
5 comments
commentsObject[] tableLines = br.lines().toArray();
Replylines()
does not exist, why?
me the same thing:
Replylines()
does not exist, why?
how to resolve mtehod lines() which not exist ?
ReplyTry readlines
ReplyIt's perfect, thank you!
Reply