如何使用 FileDialog 选择不同目录中存在的多个文件?
我需要创建一个 UI,我需要在其中添加来自不同目录的大量文件。另外,我需要创建一个复选框来指示选择哪些文件以进行进一步操作。
我尝试使用 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();
}
}