文件选择器:获取选定的后缀
我有以下方法。在文件选择器中,我可以选择一个文件,或者只需输入名称即可创建一个新文件。这工作正常。
但是如何以编程方式检查在下拉列表中选择了哪个扩展名?
我需要它将它附加到文件名中。并根据文件格式来区分要做什么。
或者是否有更简单的文件选择器解决方案?
提前致谢!
public static String startNewFile(Viewer parentFrame) {
File savedFile = null;
String savedFileName = null;
int extIndex = -1;
String tempExt = null;
String savedPath = null;
final StringBuffer sb = new StringBuffer(128);
try {
String ext1 = "a";
String ext2 = "b";
String ext3 = "c";
fileFilter.removeAllExtensions();
fileFilter.addExtension(ext1);
fileFilter.addExtension(ext2);
fileFilter.addExtension(ext3);
fileChooser.setFileFilter(fileFilter);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setDialogTitle("Log file name");
final File directory = new File(Logger.getLogDirectory());
if (!directory.exists()) {
directory.mkdir();
}
fileChooser.setCurrentDirectory(directory);
final int returnVal = fileChooser.showSaveDialog(parentFrame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
savedFile = fileChooser.getSelectedFile();
savedFileName = savedFile.getName();
extIndex = savedFileName.lastIndexOf(".");
tempExt = savedFileName.substring(extIndex + 1);
savedPath = (fileChooser.getSelectedFile()).getAbsolutePath();
if (!ext.equals(tempExt)) {
sb.append(savedPath);
sb.append(".");
sb.append(ext);
savedPath = sb.toString();
}
if (!setWriteFileName(savedPath)) {
return null;
}
} else {
return null;
}
} catch (IOException e) {
e.printStackTrace();
ErrorPopUp.setMessage(e.toString()).setVisible(true);
return null;
}
return savedPath;
}
I have the following Method. In the fileschooser I can select a file or just type in the name to create a new one. This is working fine.
But how can I check programmatically which extension was chosen in the drop down list?
I need it to append it to the filename. And to distinguish what to do depending on the fileformat.
Or is there an easier solution with a filechooser?
Thanks in advance!
public static String startNewFile(Viewer parentFrame) {
File savedFile = null;
String savedFileName = null;
int extIndex = -1;
String tempExt = null;
String savedPath = null;
final StringBuffer sb = new StringBuffer(128);
try {
String ext1 = "a";
String ext2 = "b";
String ext3 = "c";
fileFilter.removeAllExtensions();
fileFilter.addExtension(ext1);
fileFilter.addExtension(ext2);
fileFilter.addExtension(ext3);
fileChooser.setFileFilter(fileFilter);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setDialogTitle("Log file name");
final File directory = new File(Logger.getLogDirectory());
if (!directory.exists()) {
directory.mkdir();
}
fileChooser.setCurrentDirectory(directory);
final int returnVal = fileChooser.showSaveDialog(parentFrame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
savedFile = fileChooser.getSelectedFile();
savedFileName = savedFile.getName();
extIndex = savedFileName.lastIndexOf(".");
tempExt = savedFileName.substring(extIndex + 1);
savedPath = (fileChooser.getSelectedFile()).getAbsolutePath();
if (!ext.equals(tempExt)) {
sb.append(savedPath);
sb.append(".");
sb.append(ext);
savedPath = sb.toString();
}
if (!setWriteFileName(savedPath)) {
return null;
}
} else {
return null;
}
} catch (IOException e) {
e.printStackTrace();
ErrorPopUp.setMessage(e.toString()).setVisible(true);
return null;
}
return savedPath;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 getFileFilter() ,它应该返回当前选定的文件过滤器。
You can use getFileFilter() which should return the currently selected file filter.