Java JFileChooser使文件名在showDialog box中不可导入
我正在处理一个任务,它使用JFileChooser允许用户选择一个文件,并将该文件复制到用户选择的任何文件夹中。我能够让编程自动复制用户选择的具有相同名称和.bak扩展名的文件 我想做的是允许用户为要复制到的文件选择一个目标文件夹,而不是使用showSaveDialog,我发现我可以使用showDialog并将所有内容设置为仅目录但是,我想知道是否有一种方法可以消除允许用户创建文件名的区域?或者将其设置为不可编辑?我将其更改为showOpenDialog,但这会让用户选择一个文件,而我找不到方法使该目录仅为 谢谢你的帮助!我会发布我所有的代码,因为它是一个小程序,以防你想看到其他部分,我会把有问题的代码放在***之间Java JFileChooser使文件名在showDialog box中不可导入,java,swing,jfilechooser,showdialog,Java,Swing,Jfilechooser,Showdialog,我正在处理一个任务,它使用JFileChooser允许用户选择一个文件,并将该文件复制到用户选择的任何文件夹中。我能够让编程自动复制用户选择的具有相同名称和.bak扩展名的文件 我想做的是允许用户为要复制到的文件选择一个目标文件夹,而不是使用showSaveDialog,我发现我可以使用showDialog并将所有内容设置为仅目录但是,我想知道是否有一种方法可以消除允许用户创建文件名的区域?或者将其设置为不可编辑?我将其更改为showOpenDialog,但这会让用户选择一个文件,而我找不到方法
public class CopyFile extends JFrame{
private JFileChooser fc;
private JButton copyButton;
private JButton chooseFileButton;
private JButton destinationButton;
private File workingDirectory;
private JLabel sourceLabel;
private JTextArea displayCopyText;
private JLabel destinationLabel;
private JTextField sourceText;
private JLabel copyText;
private JTextField destinationText;
public static void main(String [] args) {
CopyFile go = new CopyFile();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setResizable(false);
go.setSize(500, 150);
go.setVisible(true);
}
public CopyFile() {
super("Copy a text file");
setLayout(new FlowLayout());
fc = new JFileChooser();
//Open dialog box inside project folder to make easier to find files
workingDirectory = new File(System.getProperty("user.dir"));
fc.setCurrentDirectory(workingDirectory);
//create labels and buttons for window
chooseFileButton = new JButton("CHOOSE SOURCE FILE");
destinationButton = new JButton("DESTINATION FOLDER");
copyButton = new JButton("COPY FILE");
sourceLabel = new JLabel("SOURCE FILE: ", JLabel.CENTER);
sourceText = new JTextField(15);
sourceText.setEditable(false);
destinationText = new JTextField(15);
destinationText.setEditable(false);
destinationLabel = new JLabel("DESTINATION:", JLabel.CENTER);
//JScrollPane SP = new JScrollPane();
displayCopyText = new JTextArea();
displayCopyText.setPreferredSize(new Dimension(300, 50));
displayCopyText.setRows(2);
displayCopyText.setLineWrap(true);
displayCopyText.setWrapStyleWord(true);
displayCopyText.setEditable(false);
//add everything to JFrame
add(sourceLabel);
add(sourceText);
add(chooseFileButton);
add(destinationLabel);
add(destinationText);
add(destinationButton);
//add(copyText);
add(displayCopyText);
add(copyButton);
//Create TheHandler object to add action listeners for the buttons.
TheHandler handler = new TheHandler();
chooseFileButton.addActionListener(handler);
destinationButton.addActionListener(handler);
copyButton.addActionListener(handler);
}
//Inner class to create action listeners
private class TheHandler implements ActionListener {
private File selectedDestinationFile;
private File selectedSourceFile;
private int returnVal;
public void actionPerformed(ActionEvent event) {
//Selecting a source file and displaying what the user is doing.
if(event.getSource() == chooseFileButton) {
returnVal = fc.showOpenDialog(null);
//Set the path for the source file.
if(returnVal == JFileChooser.APPROVE_OPTION) {
selectedSourceFile = fc.getSelectedFile();
sourceText.setText(selectedSourceFile.getName());
}
}//end if
**********************************************************************************
//Handle destination button.
if(event.getSource() == destinationButton) {
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setDialogTitle("CHOOSE A FOLDER ONLY, NO NEED TO NAME THE FILE TO BE COPIED.");
//I changed below from a showSaveDialog and played around with it with no luck.
returnVal = fc.showDialog(null, "Choose Destination");
if(returnVal == JFileChooser.APPROVE_OPTION) {
selectedDestinationFile = fc.getSelectedFile();
destinationText.setText(fc.getSelectedFile().getAbsolutePath());
}
}//end if
**********************************************************************************
//Handle copy button
if(event.getSource() == copyButton) {
/*
Path sourcePath = selectedSourceFile.toPath();
Path destinationPath = selectedDestinationFile.toPath();
try {
Files.copy(sourcePath, destinationPath);
} catch (IOException e) {
e.printStackTrace();
} */
String name = selectedSourceFile.getName();
name = selectedSourceFile.getName().substring(0, name.lastIndexOf(".")) + ".bak";
File destinationFile = new File(selectedDestinationFile.getParentFile(), name);
destinationText.setText(name);
try {
Files.copy(selectedSourceFile.toPath(), destinationFile.toPath());
} catch (IOException e) {
e.printStackTrace();
}
if(returnVal == JFileChooser.APPROVE_OPTION) {
displayCopyText.append("SUCCESSFULLY COPIED:\n"
+ selectedSourceFile.getName() + " ===>> " + name);
}
else {
displayCopyText.append("COPY WAS CANCELED BY USER.\n");
}
}//end if
}//end actionPerformed
}//end TheHandler class
}//end class
我认为简短的答案是否定的。无论出于什么原因,如果您想限制用户,您都应该以任何方式检查返回结果,查找
null
结果和无效结果(!file.exists()| |!file.isDirectory()
)总之,JFileChooser类有一个常量“DIRECTORIES\u ONLY”,可能与fileSelectionMode方法一起使用。或fc.fileSelectionMode(仅限于JFileChooser.DIRECTORIES_);任务是自动将一个文件复制到一个同名的.bak文件中。我希望用户能够将所述文件保存在他们想要的任何目录中,但如果他们在其中添加文件名,则完全没有意义,因为程序使用源文件名并向其中添加.bak。我只是不确定是否有我丢失的另一个对话框,或者是否有自定义showDialog的方法来摆脱输入文件名的位置。我不知道你说的最后一部分是什么意思……我是否也应该检查源文件的文件?目标文件不应该有什么区别,因为即使用户选择了一个文件,它也只会在他们选择的文件的文件夹中复制一个.bak。嘿,克里夫,谢谢你,但是,我只使用目录。这并不是什么大问题,因为程序会自动创建一个与源文件同名的.bak,我只是想通过取消用户输入文件名的选项来减少用户的困惑。