Selecting Multiple files in FileChooser
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);
开发者_如何学C 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();
}
}
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.
JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
Thats all you have to do, easy.
精彩评论