• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

How to create select files/directories dialog box with alphabetical order in JAVA?

In my GUI that I develop in Java, I needed to have a dialog box that will provide me to select both multiple files and multiple folders at the same time. Therefor, I did not use the built in directory or file dialog box. Instead, I have custom dialog box that provides me to do these.

Even though, it does not look as good as the original widgets it kind of does what I need.

However, I realized two problems about it recently.

It does not show the directories and the files in the alphabetical order. They are in random order. However, I know that they are in alphabetical order in my machine as expected.

 How can I make it in alphabetical order? (Please see the code at the end of the page.)

It does not work as fast as the original file dialog or directory dialog. Especially, if the files and directories that are on the network does not show up and the dialog box halts.

How can I make it work faster?

If there is a better solution than just making changes on my code, I would also appreciate that. Or, any recommendation would also be helpful on the code that I am sending below:

public class FileTree {

   public static SelectedFileFoldersModel model = new SelectedFileFoldersModel();
    public String getFileFolders()
        return model.getFileOrFolderData();
public static void main (String [] args) {
  final Display display = Display.getDefault();
  final Shell shell = new Shell (display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
  shell.setText ("Select Files/Folders");
  GridLayout gridLayout = new GridLayout(2, false);
  Rectangle screen = shell.getClientArea();
  shell.setLocation(800, 500);
  GridData FileTree = new GridData(GridData.FILL_HORIZONTAL);
  FileTree.horizontalSpan = 2;
  final Tree tree = new Tree (shell, SWT.BORDER | SWT.MULTI);
  File [] roots = File.listRoots ();
  for (int i=0; i<roots.length; i++) {
    TreeItem root = new TreeItem (tree, 0);
    root.setText (roots [i].toString ());
    root.setData (roots [i]);
    new TreeItem (root, 0);
  tree.addListener (SWT.Expand, new Listener () {
    public void handleEvent (final Event event) {
      final TreeItem root = (TreeItem) event.item;
      TreeItem [] items = root.getItems ();
      for (int i= 0; i<items.length; i++) {
        if (items [i].getData () != null) return;
        items [i].dispose ();
      File file = (File) root.getData ();
      File [] files = file.listFiles ();
      if (files == null) return;
      for (int i= 0; i<files.length; i++) {
        if (files[i].isHidden()) continue;
        TreeItem item = new TreeItem (root, 0);
        item.setText (files [i].getName ());
        item.setData (files [i]);
        if (files [i].isDirectory()) {
          new TreeItem (item, 0);
  Point size = tree.computeSize (SWT.DEFAULT, SWT.DEFAULT);
  int width = Math.max (300, size.x+40);
  int height = Math.min (300, size.y+40);
  shell.setSize (shell.computeSize (width, height));
  shell.addShellListener(new ShellListener() {

      public void shellIconified(ShellEvent e) {
      public void shellDeiconified(ShellEvent e) {
      public void shellDeactivated(ShellEvent e) {
      public void shellClosed(ShellEvent e) {
      public void shellActivated(ShellEvent e) {
  // ----------- third row -----------------
  Button ok = new Button(shell, SWT.PUSH);
  GridData dataOK = new GridData(SWT.FILL, SWT.FILL, true, false);
  ok.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent event) {
        TreeItem[] selection = tree.getSelection();
        for (int i = 0; i < selection.length; i++) {
            File file = (File)selection[i].getData();
            if (file.isDirectory()) {
                model.storeFileOrFolder("-d " + file.getPath() + " -r");
            } else if (file.isFile()) {
                model.storeFileOrFolder("-f " + file.getPath());

  Button cancel = new Button(shell, SWT.PUSH);
  GridData dataCancel = new GridData(SWT.FILL, SWT.FILL, true, false);
  cancel.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent event) {
  shell.open ();
  while (!shell.isDisposed ()) {
    if (!display.readAndDispatch ()) display.sleep (); 

Open in new window

1 Solution
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You should just need to sort the arrays returned by the various "File" class/object methods. So between lines 25 & 26, add this (you will need to import java.util.Arrays)...

Open in new window

And then between 44 & 45, add this...

Open in new window

Dave BaldwinFixer of ProblemsCommented:
I know that they are in alphabetical order in my machine as expected.
No they are not.  Windows Explorer sorts them for you.  The file explorers and directory listers in other operating systems do too.  The array sorts mccarl shows above are what you need.

I've done file listings in other languages and I encounter the same situation.  Files are listed in the directory in the order that they were created except when one is deleted then the next file entry goes in that spot.
No they are not.  Windows Explorer sorts them for you.  
That's right.

Files are listed in the directory in the order that they were created
Again, that depends on the view. There's actually no such thing as a file anyway
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now