3

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

Community
  • 1
  • 1
Winsanity
  • 61
  • 1
  • 5
  • Use a SwingWorker, for [example](http://stackoverflow.com/questions/17414109/populate-jtable-with-large-number-of-rows/17415635#17415635) – MadProgrammer May 25 '16 at 20:09

1 Answers1

3

The Timer's action handlers is executed on the EDT which means that you're blocking the EDT while you're reading the file. In this case, the only thing that the timer is doing is to delay the file reading by 2 seconds.

You don't need a timer to do this, you can do something like this instead.

public void addData(){
    final DefaultTableModel model = (DefaultTableModel) resultTable.getModel();
    new Thread(new Runnable(){
        public void run(){
            FileInputStream fstream = null;
            try {
                fstream = new FileInputStream("\\data.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String line;
                while((line = br.readLine()) != null){
                    final String ln = line;
                    SwingUtilities.invokeAndWait(new Runnable(){
                        // run on EDT thread.
                        public void run(){
                            model.addRow(new Object[]{ln});
                        }
                    }); 
                    synchronized(Thread.currentThread()){
                        Thread.currentThread().wait(2000);// pause for 2 second.
                    }
                }
            } catch (FileNotFoundException ex) {
                ...
            } catch (IOException ex) {
                ....
            } finally {
                if(fstream!=null){
                    try{
                        fstream.close();
                    }catch (IOException e) {
                        ....
                    }
                }
            }
        }
    }).start();
}
Titus
  • 22,031
  • 1
  • 23
  • 33