简体   繁体   中英

Auto Refresh in Swing (Jtable)

I wrote a code which is get text from text area and save to file after that get data from file and show at jtable but I want to do that automatically when I add a new text to file it should be automatically show at the jtable does any one can help me?

try {
                    BufferedReader br = new BufferedReader(new FileReader(new File ("Member.txt")));


                    Object [] row = {id, firstname, lastname, age};

                    DefaultTableModel dtm = (DefaultTableModel) table.getModel();



                    Object[] tableLines = br.lines().toArray();

                    for(int i = 0; i < tableLines.length; i++)
                    {
                        String line = tableLines[i].toString().trim();
                        String[] dataRow = line.split(" ");
                        dtm.addRow(dataRow);
                    }


                } catch (Exception ex) {
                    System.out.println(ex);
                }

this code help me to show data from file at jtable.

Your starting point is: using a watch service to determine when that file changed on disk.

Then you have to read the whole file and figure what changed (for example by keeping a "copy" of all lines of the file in memory).

Then you extract the newly added lines and add them to your table model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM