在 FileChooser 中选择多个文件
如何使用 FileDialog 选择不同目录中的多个文件?
我需要创建一个用户界面,在其中需要添加来自不同目录的大量文件。另外,我需要创建一个复选框来指示选择哪些文件进行进一步操作。
我尝试使用 SWT 来实现这一点,但没有得到预期的结果。事实上,我无法选择每个文件,因为我无法将复选框添加到每个文件。由于我是SWT新手,我很难理解。
我的代码在这里:
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class DirFileSelection {
Display display = new Display();
Shell shell = new Shell(display);
FileDialog fileDialog = new FileDialog(shell, SWT.MULTI);
DirectoryDialog directoryDialog = new DirectoryDialog(shell);
String sb = new String("");
// the label used to display selected dir/file.
Label label = new Label(shell, SWT.BORDER | SWT.WRAP);
Button buttonSelectDir;
Button buttonSelectFile;
Button validate = new Button(shell, SWT.PUSH);;
Button reset = new Button(shell, SWT.PUSH);;
String selectedDir;
String fileFilterPath = "F:/jdk1.5";
public DirFileSelection() {
shell.setSize(1000, 1000);
label.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
label.setText("Select a dir/file by clicking the buttons below.");
buttonSelectDir = new Button(shell, SWT.PUSH);
buttonSelectDir.setText("Select a directory");
buttonSelectDir.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
directoryDialog.setFilterPath(selectedDir);
directoryDialog.setMessage("Please select a directory and click OK");
String dir = directoryDialog.open();
if (dir != null) {
// label.setText("Selected dir: " + dir);
selectedDir = dir;
int decide = 0;
System.out.println(sb);
String[] str = sb.split("\n");
for (int i = 0; i < str.length; i++) {
// for (int j = i; j < str.length; j++) {
// if(i==j){
// continue;
// }
// else if(str[i].equals(str[j])){
// System.out.println("dup");
// x=false;
// }
// }
if (dir.equals(str[i])) {
System.out.println("dup");
decide = 1;
break;
}
}
if (decide == 0) {
sb = sb.concat(dir + "\n");
label.setText(sb);
}
}
// label.setText(sb.toString());
}
});
buttonSelectFile = new Button(shell, SWT.PUSH);
buttonSelectFile.setText("Select a file/multiple files");
buttonSelectFile.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
fileDialog.setFilterPath(fileFilterPath);
fileDialog.setFilterExtensions(new String[] { "*.*" });
fileDialog.setFilterNames(new String[] { "ALL Files" });
String firstFile = fileDialog.open();
if (firstFile != null) {
fileFilterPath = fileDialog.getFilterPath();
String[] selectedFiles = fileDialog.getFileNames();
for (int i = 0; i < selectedFiles.length; i++) {
// sb.append(selectedFiles[i] + "\n");
sb = sb.concat(selectedFiles[i] + " \n");
}
}
label.setText(sb.toString());
}
});
validate.setText("Validate");
validate.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event arg0) {
// TODO Auto-generated method stub
}
});
reset.setText("Reset");
reset.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event arg0) {
fileDialog.setFileName(null);
System.out.println(fileDialog.getFileName());
sb = "";
shell.update();
label.setText(sb.toString());
}
});
label.setBounds(0, 0, 800, 180);
buttonSelectDir.setBounds(0, 185, 200, 20);
buttonSelectFile.setBounds(200, 185, 200, 20);
validate.setBounds(400, 185, 200, 20);
reset.setBounds(600, 185, 200, 20);
shell.pack();
shell.open();
// textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new DirFileSelection();
}
}
How to select multiple files present in different directories using FileDialog?
I need to create a UI where I need to add lots of files from different directories. Also, I need to create a checkbox to indicate which files are selected for further action.
I tried to achieve this using SWT, but I didn't get the desired result. In fact I am not able to select each file since I am not able to add the checkbox to each file. Since I am new to SWT, I have difficulty in understanding.
My code is here:
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class DirFileSelection {
Display display = new Display();
Shell shell = new Shell(display);
FileDialog fileDialog = new FileDialog(shell, SWT.MULTI);
DirectoryDialog directoryDialog = new DirectoryDialog(shell);
String sb = new String("");
// the label used to display selected dir/file.
Label label = new Label(shell, SWT.BORDER | SWT.WRAP);
Button buttonSelectDir;
Button buttonSelectFile;
Button validate = new Button(shell, SWT.PUSH);;
Button reset = new Button(shell, SWT.PUSH);;
String selectedDir;
String fileFilterPath = "F:/jdk1.5";
public DirFileSelection() {
shell.setSize(1000, 1000);
label.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
label.setText("Select a dir/file by clicking the buttons below.");
buttonSelectDir = new Button(shell, SWT.PUSH);
buttonSelectDir.setText("Select a directory");
buttonSelectDir.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
directoryDialog.setFilterPath(selectedDir);
directoryDialog.setMessage("Please select a directory and click OK");
String dir = directoryDialog.open();
if (dir != null) {
// label.setText("Selected dir: " + dir);
selectedDir = dir;
int decide = 0;
System.out.println(sb);
String[] str = sb.split("\n");
for (int i = 0; i < str.length; i++) {
// for (int j = i; j < str.length; j++) {
// if(i==j){
// continue;
// }
// else if(str[i].equals(str[j])){
// System.out.println("dup");
// x=false;
// }
// }
if (dir.equals(str[i])) {
System.out.println("dup");
decide = 1;
break;
}
}
if (decide == 0) {
sb = sb.concat(dir + "\n");
label.setText(sb);
}
}
// label.setText(sb.toString());
}
});
buttonSelectFile = new Button(shell, SWT.PUSH);
buttonSelectFile.setText("Select a file/multiple files");
buttonSelectFile.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
fileDialog.setFilterPath(fileFilterPath);
fileDialog.setFilterExtensions(new String[] { "*.*" });
fileDialog.setFilterNames(new String[] { "ALL Files" });
String firstFile = fileDialog.open();
if (firstFile != null) {
fileFilterPath = fileDialog.getFilterPath();
String[] selectedFiles = fileDialog.getFileNames();
for (int i = 0; i < selectedFiles.length; i++) {
// sb.append(selectedFiles[i] + "\n");
sb = sb.concat(selectedFiles[i] + " \n");
}
}
label.setText(sb.toString());
}
});
validate.setText("Validate");
validate.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event arg0) {
// TODO Auto-generated method stub
}
});
reset.setText("Reset");
reset.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event arg0) {
fileDialog.setFileName(null);
System.out.println(fileDialog.getFileName());
sb = "";
shell.update();
label.setText(sb.toString());
}
});
label.setBounds(0, 0, 800, 180);
buttonSelectDir.setBounds(0, 185, 200, 20);
buttonSelectFile.setBounds(200, 185, 200, 20);
validate.setBounds(400, 185, 200, 20);
reset.setBounds(600, 185, 200, 20);
shell.pack();
shell.open();
// textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new DirFileSelection();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道有什么可能的方法可以从不同的文件夹中选择更多文件(不仅在 SWT 中,而且在 Swing 甚至操作系统中)。
您的方法是好的,让用户通过在已选择的文件之后添加新的选定文件(在之前的文件对话框中)来选择他/她想要的任意数量的文件。将其写到 GUI 中文件选择按钮附近。
I'm not aware of any possible way to choose more files from different folders (not just in SWT, but in Swing or even OS's).
Your approach is okey, let user select as many files as he/she wants by adding new selected files after those already selected (in previous file dialogs). Write this down to your GUI near the button for file selection.
这就是您所要做的一切,简单。
Thats all you have to do, easy.