1

I've got a JTable that is filled by Vectors. These Vectors get their content by a loop, listing all files in a specific folder. Now I want to edit a Cell (via the GUI) that contains a filename and the real File on the HD should be renamed too. So how do I archive this?

Here's my code for the method that fills the JTable.

private void reloadFiles(){
    vecVectors.clear();
    if (Variables.pathToFiles != null) {
        Variables.listOfFiles.clear();
        Variables.listOfFiles = listFilesForFolder(Variables.pathToFiles);

        for (File file : Variables.listOfFiles) {
            Vector<String> temp = new Vector<String>();
            temp.add(file.getName());
            vecVectors.add(temp);
        }

        table.removeAll();
        table = new JTable(vecVectors, vecHeaders);

        this.remove(listScroller);
        listScroller.removeAll();
        listScroller = new JScrollPane(table);
        listScroller.setPreferredSize(new Dimension(950, 450));

        this.add(listScroller);

        System.out.println("Reloaded");
    }
}
dic19
  • 17,821
  • 6
  • 40
  • 69
DirtyNative
  • 2,553
  • 2
  • 33
  • 58

1 Answers1

1

Instead of using vectors to fill your table, you can use a more sophisticated table model implementation that allows you to work directly with File objects as rows in your table. I'm talking about either DataObjectTableModel or Rob Camick's RowTableModel.

So, let's say you have the table model implementation solved and each row in your table is a File object. Now, I wouldn't make any cell editable and let the table just display files info, considering:

  1. There are many factors that can go wrong (i.e: invalid or existent file name)
  2. It's not a TableModel responsibility to reflect the update in the HD.
  3. It can be done using TableModelListener but the events are fired once the model has changed, so if you have troubles updating the file name in the HD then you have to revert the table model's changes. Not so easy though.

In order to re-name a file, I'd attach a MouseListener to the table and show a pop up dialog to let the user input the new file name. Finally use the File API to rename the file and update the table model reflecting these changes.

Snippet

final DataObjectTableModel<File> model = new DataObjectTableModel<File>(header) {
    // Override getValueAt() and getColumnClass() here
};

final JTable table = new JTable(model);
table.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {

        if (e.getClickCount() >= 2 && !e.isPopupTrigger()) {

            int selectedRow = table.rowAtPoint(e.getPoint());

            if (selectedRow != -1) {
                String newName = JOptionPane.showInputDialog (
                        null 
                      , "Please input the new name"
                      , "Rename file"
                      , JOptionPane.INFORMATION_MESSAGE
                );

                if (newName != null) {
                    int modelIndex = table.convertRowIndexToModel(selectedRow);

                    File file = model.getDataObject(modelIndex);

                    // Maybe this part should be done
                    // using a SwingWorker to avoid blocking the EDT
                    String newPath = file.getParent() + File.separator + newName;
                    File newFile = new File(newPath);

                    if (file.renameTo(newFile)) {
                        model.deleteDataObject(modelIndex); // remove the old file
                        model.insertDataObject(newFile, modelIndex); // insert the new file in the same index
                    } else {
                        JOptionPane.showMessageDialog (
                                null
                              , "An error happened trying to rename file " + file.getName()
                              , "Error!"
                              , JOptionPane.WARNING_MESSAGE
                        );
                    }
                }

            } // if (selectedRow != -1)

        } // if (e.getClickCount() >= 2 && !e.isPopupTrigger())

    }
});
Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • You are welcome @DanielDirtyNativeMartin If this question worked for you then don't forget to mark it as accepted: [What does it mean when an answer is "accepted"?](http://stackoverflow.com/help/accepted-answer) – dic19 Dec 18 '14 at 19:47