How to select multiple files and/or folders at the same time in a GUI by using SWT?
Hi,
I have a GUI and in this GUI when you click on a icon, it should open a dialog box and you should be able to select files and/or folders.
The following code can do this this for files and I can convert this to work for files by using FileDialog instead of DirectoryDialog. But I couldn't make it work both for files and directories at the same time.
Can you please help?
The code is below:
import org.eclipse.swt.widgets.DirectoryDialog;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.ToolItem;public class ToolBarListener implements Listener { private Shell shell; @Override public void handleEvent(Event event) { ToolItem toolItem = (ToolItem) event.widget; if (toolItem == Gui.item1) { System.out.println("item1 clicked..."); DirectoryDialog dlg = new DirectoryDialog(shell); String dir = dlg.open(); System.out.println("chosen dir : " + dir); } else if (toolItem == Gui.item13) { Gui.swichTabChecks(); } else if (toolItem == Gui.item23) { Gui.swichTabResults(); } else if (toolItem == Gui.item3333333) { Gui.swichTabFiles(); } } public void setShell(Shell shell) { this.shell = shell; }}
Given that you've put only Java as a tag, but given that I know zero about SWT, in Java a file can be both a file, or a directory, so this ought to be the first thing to check - that you are in fact not seeing both already.
} else if (toolItem == Gui.item111) {
FileDialog fd = new FileDialog(shell, SWT.MULTI);
String firstFile = fd.open();
if (firstFile != null) {
String[] selectedFiles = fd.getFileNames();
for (int ii = 0; ii < selectedFiles.length; ii++ ) {
System.out.println("selected file : " + selectedFiles[ii]);
}
}
}
0
TolgarAuthor Commented:
So, now I have this piece of code with the addition of your suggestion and with this code, I cannot select the files and the folders by using the same icon.
What it does is, it selects the folders because the DirectoryDialog is before the FileDialog in the if statement.
If I remove this piece of if statement then the new code that you suggested can only select multiple files but not the folders.
This is the code I have:
import org.eclipse.swt.SWT;import org.eclipse.swt.widgets.DirectoryDialog;import org.eclipse.swt.widgets.FileDialog;import org.eclipse.swt.widgets.Event;import org.eclipse.swt.widgets.Listener;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.ToolItem;public class ToolBarListener implements Listener { private Shell shell; @Override public void handleEvent(Event event) { ToolItem toolItem = (ToolItem) event.widget; if (toolItem == Gui.item1) { System.out.println("item1 clicked..."); DirectoryDialog dlg = new DirectoryDialog(shell); String dir = dlg.open(); System.out.println("chosen dir : " + dir); } else if (toolItem == Gui.item13) { Gui.swichTabChecks(); } else if (toolItem == Gui.item23) { Gui.swichTabResults(); } else if (toolItem == Gui.item3333333) { Gui.swichTabFiles(); } else if (toolItem == Gui.item1) { FileDialog fd = new FileDialog(shell, SWT.MULTI); String firstFile = fd.open(); if (firstFile != null) { String[] selectedFiles = fd.getFileNames(); for (int ii = 0; ii < selectedFiles.length; ii++ ) { System.out.println("selected file : " + selectedFiles[ii]); } } } } public void setShell(Shell shell) { this.shell = shell; }}
My requirement is to select multiple files and/or directories by using the same icon. Also, files and directories should be selected at the same time in one shot.
So as an example, I have the following files and folders in directory A.
The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.
One of a set of tools we're offering as a way of saying thank you for being a part of the community.
take a look at the attached file. if you run it, you can choose multiple files and folder in the same time. when you close the component selected files and folders are dumped to the console. You can format the output according to your needs. FileTree.java
0
TolgarAuthor Commented:
Hi Valeri,
I created the file you sent me, FileTree.java.
I added the following line to ToolBarListener.java:
if (toolItem == Gui.item1) { Gui.selectFileFolders(); }