在jfilechooser中重命名文件而不使用inputdialog

wribegjk  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(201)

我已经用java编写了一个gui,它允许您保存一个文件。在jfilechooser窗口中,还可以重命名、删除或创建新文件。到目前为止,始终会打开一个新的输入对话框来输入文件的新名称。如何在jfilechooser窗口中输入新文件的名称而不打开新的joptionpane inputdialog?
非常感谢

private void contextMenu() {
        try {
            popup = new JPopupMenu();
            ActionListener menuListener = new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed.");
                    if (event.getActionCommand().equals("Delete")) {
                        // your logic here
                        File file = fc.getSelectedFile();
                        if (file != null) {
                            if (askConfirm() == JOptionPane.YES_OPTION) {
                                try {
                                    String deleted = file.getName();
                                    String location = file.getParent();
                                    Files.delete(file.toPath());
                                    log.info("Deleted " + deleted + " in the directory " + location);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                fc.rescanCurrentDirectory();
                            }
                        }
                    } else if (event.getActionCommand().equals("Rename")) {
                        File file = fc.getSelectedFile();
                        String path = file.getParent();

                        String rename = JOptionPane.showInputDialog("Please enter name:");
                        if (file.exists()) {
                            String oldName = file.getName();
                            file.renameTo(new File(path + "/" + rename));
                            String newName = file.getName();
                            String directory = file.getParent();
                            log.info("Changed the name from " + oldName + " to " + newName + " in the directory "
                                    + directory);
                        }
                        fc.rescanCurrentDirectory();
                    } else if (event.getActionCommand().equals("New Folder")) {
                        File file = fc.getCurrentDirectory();
                        String path = file.getPath();
                        String directory = file.getParent();
                        String name = JOptionPane.showInputDialog("Please enter name:");
                        name = name.equals("") || name.equals(null) ? "Unbenannt" : name;
                        boolean dir = new File(path + "/" + name).mkdir();
                        if (dir) {
                            log.info("Created Directory " + name + " in the directory " + directory);
                        }
                        fc.rescanCurrentDirectory();
                    }
                }
            };

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题