I am creating a file manager in java.I have a problem when I am trying to rename the files and folders.I am taking the new filename from user input then I am trying to rename the file.
popupMenu.rename.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e) {
list.getSelectedValue().toString();
JFrame frame = new JFrame("Rename");
final JTextField textField=new JTextField();
String s = null;
File file2 = new File("newname");
textField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
s = textField.getText();
}
});
Object o = list.getModel().getElementAt(list.getSelectedIndex());
File file = new File(o.toString());
//FileUtils.moveFile(file, newFile);
file.renameTo(file2);
//Files.move(file.toPath(),file.toPath().resolveSibling(textField.getText()));
frame.add(textField);
frame.setLocationRelativeTo(frame);
frame.setSize(200,60);
frame.setVisible(true);
}
});
Please suggest anything mistakes I made.
Also, is there a way I could rename the file similiar to windows by clicking on text.I am displaying the files and folders name by setText(fileName);.Here is my project screenshot