Akshay Dhavle
Akshay Dhavle

Reputation: 3

jtable data getting replaced with previous result when double clicked

I am developing one application in Swing in which I scan data from some files and show it in JTable on UI and when I double click on row respective file gets opened. Code of 'Scan' button is as follows,

JButton btnStart = new JButton("Scan");
    btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {                         

                File dir = new File(chooser.getSelectedFile(), chooser.getSelectedFile().getAbsolutePath());                
                System.out.println("dir " + dir);
                File[] firstLevelFiles = chooser.getSelectedFile().listFiles();
                if (firstLevelFiles != null && firstLevelFiles.length > 0) {
                    for (File aFile : firstLevelFiles) {
                        if (aFile.isDirectory()) {
                            listDirectory(aFile.getAbsolutePath(), level + 1);
                        } else {                            
                            fileExt=aFile.getName().substring(aFile.getName().lastIndexOf(".")+1);                                                      
                            for (String string : selectedExt) {
                                if(string.equals(fileExt)){ 
                                    fileList.add(aFile);    
                                }                                                       
                            }
                        }
                    }
                }                   

                MyFileReader myFileReader=new MyFileReader();

                new Thread(new Runnable(){
                    @Override
                    public void run(){

                        fileMap=myFileReader.fileReader(fileList, filepath);
                       if(!fileMap.isEmpty()){
                         SwingUtilities.invokeLater(new Runnable(){
                             @Override public void run(){

                                    int tableSize=myFileReader.getTableSize();

                                    tableData=new String[tableSize][4];
                                    Iterator it = fileMap.entrySet().iterator();
                                    int j=0;
                                    int k=0;                                        
                                    while (it.hasNext()) {
                                        Map.Entry pair = (Map.Entry) it.next();                                         
                                        List<String> ls = (List<String>) pair.getValue();
                                        for (int i = 1; i <= ls.size(); i++) {
                                            if(k==1){
                                                k++;
                                                i--;
                                                continue;
                                            }

                                            tableData[j][1]=pair.getKey().toString();
                                            tableData[j][k]=ls.get(i-1).toString();
                                            k++;
                                            if(k==4){
                                                k=0;
                                                j++;
                                            }
                                        }
                                    }                                                                               
                                     model = new DefaultTableModel(tableData, columns)
                                      {
                                        public boolean isCellEditable(int row, int column)
                                        {
                                          return false;//This causes all cells to be not editable
                                        }
                                      };

                                    table = new JTable(model);                                                                  
                                    table.setEnabled(true);
                                    JTableHeader header = table.getTableHeader();
                                    header.setBackground(Color.LIGHT_GRAY);
                                    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                                    table.setCellSelectionEnabled(true);
                                    table.getColumnModel().getColumn(0).setPreferredWidth(5);
                                    table.getColumnModel().getColumn(1).setPreferredWidth(400);
                                    table.getColumnModel().getColumn(2).setPreferredWidth(5);
                                    table.getColumnModel().getColumn(3).setPreferredWidth(5);

                                    pane = new JScrollPane(table);
                                    pane.setBounds(70,250,1000,300);
                                    pane.setVisible(true);
                                    contentPane.add(pane,BorderLayout.CENTER);

                                    table.addMouseListener(new MouseAdapter() {
                                         public void mouseClicked(MouseEvent e) {
                                            if (e.getClickCount() == 2) {
                                                int columnChk=table.getSelectedColumn();
                                                if (columnChk == 1) {
                                                    String fpath = getSelectedData(table);

                                                    try {
                                                        Runtime runtime = Runtime.getRuntime();
                                                        runtime.exec("explorer " + fpath);

                                                    } catch (IOException e1) {
                                                        // TODO Auto-generated catch block
                                                        e1.printStackTrace();
                                                    }
                                                }
                                            }
                                         }
                                      });
                                 scanProg.setText("Scanning Completed");
                           }
                          });
                       }else {
                            scanProg.setText("No codebase found");
                       }
                    }
                    }).start();                                                     
            }


        }
    });
    btnStart.setBounds(324, 154, 89, 23);
    contentPane.add(btnStart);

Now, when I rescan files without closing frame new data is getting filled in JTable but once I double click on any row it's data is getting replaced with previous one which is not desired.I want to clear data from table from privious scan. Already have tried JTable.removeall() but it didnt helped. Screenshot of table is here Any idea?

Upvotes: 0

Views: 620

Answers (1)

camickr
camickr

Reputation: 324137

table = new JTable(model);    

Don't keep creating new components. Instead you can update the data of the existing table by using:

table.setModel( model );

.I want to clear data from table from privious scan

Or instead of creating a completely new DefaultTableModel you can use the exiting model.

You clear the data by using:

model.setRowCount( 0 );

and you add rows of data by using:

model.addRow(...);

Upvotes: 1

Related Questions