Java Import Text File Data To JTable
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);
}
}
Posting Komentar untuk "Java Import Text File Data To JTable"