Basically I have a method to read from a text file and add lines to a jTable
. That has been taken care of and everything works fine. What I want is the add row effect. I want the jTable to add row by row instead of all rows at once.
I have found similar solution using java.swing.Timer, but it does not work for me.
So below I have a method to read from the text file, add to jTable and the Timer to pause the inserting rows process (my intended purpose) for every row. The Timer works fine, but instead of delaying the row, it delays the whole table. So when I click on the button, the table waits for 2 seconds and spits out the entire rows. How can I achieve the add row by row effect like the UPDATE #2 of peekillet's. Any helps would be appreciated!
private void addActionPerformed(java.awt.event.ActionEvent evt) {
addData();
}
public void addData(){
Timer timer = new Timer(2000, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
FileInputStream fstream = new FileInputStream("\\data.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String line;
DefaultTableModel model = (DefaultTableModel) resultTable.getModel();
while((line = br.readLine()) != null){
model.addRow(new Object[]{line});
//System.out.println(strLine);
}
fstream.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(PIIFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PIIFrame.class.getName()).log(Level.SEVERE, null, ex);
}
repaint();
}
});
timer.setRepeats(false);
timer.start();
}
Reference: peeskillet's solution