![](/img/trans.png)
[英]How to set all selected file absolute paths/names by JFileChooser into a JTextField
[英]How do you get the absolute paths for multiple file selections using jfilechooser from an array of file names in java
以下是附加多个文件的源代码。
public void doAttachFile(){
try {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
int selection = fileChooser.showOpenDialog(null);
if(selection == JFileChooser.APPROVE_OPTION){// if open button is clicked
File [] selectedFile = fileChooser.getSelectedFiles();
}
}catch(Exception e){
JOptionPane.showMessageDialog(this,"Error attaching files\n"+e.toString,"Error",JOptionPane.ERROR_MESSAGE);
}
}
如何从数组中获取所选文件的绝对路径?
您可以遍历每个File
对象并获取File
的绝对路径,如下所示:
File [] selectedFile = fileChooser.getSelectedFiles();
for(File file : selectedFile) {
String absolutePath = file.getAbsolutePath(); //gives the absolute path
System.out.println(absolutePath);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.