Search code examples
javaswtswitch-statementmenuitemcase-statement

Turning a File Array into a number of MenuItems


I have written the following piece of code for a dropdown menu next a text box, ProjectName. When the button is pressed, a drop-down menu will open with a MenuItem for each file in a specified location. When the user presses the MenuItem the text in ProjectName will be changed to the name of the file.

static File file = new File("C:\\GradeCalc\\java\\saves\\");
static File[] listOfFiles = file.listFiles();

 Button btn = new Button(Name, SWT.FLAT|SWT.ARROW|SWT.DOWN);
      btn.setBounds(280, 50, 20, 20);
      btn.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                super.widgetSelected(e);

                Menu menu = new Menu(Save_Dialog, SWT.POP_UP);


                for (int i = 0; i < listOfFiles.length; i++) {
                     switch(i) {
                      case 0:  MenuItem item1 = new MenuItem (menu, SWT.PUSH);
                            item1.setText(listOfFiles[0].getName());
                            item1.addSelectionListener(new SelectionAdapter() {
                                public void widgetSelected(SelectionEvent e) {
                                    ProjectName.setText(listOfFiles[0].getName());
                                }});
                               break;
                      case 1:   MenuItem item2 = new MenuItem (menu, SWT.PUSH);
                            item2.setText(listOfFiles[1].getName());
                            item2.addSelectionListener(new SelectionAdapter() {
                                public void widgetSelected(SelectionEvent e) {
                                    ProjectName.setText(listOfFiles[1].getName());
                                }});
                               break;
                      case 2:  MenuItem item3 = new MenuItem (menu, SWT.PUSH);
                            item3.setText(listOfFiles[2].getName());
                            item3.addSelectionListener(new SelectionAdapter() {
                                public void widgetSelected(SelectionEvent e) {
                                    ProjectName.setText(listOfFiles[2].getName());
                                }});
                             break;
                      case 3:   MenuItem item4 = new MenuItem (menu, SWT.PUSH);
                            item4.setText(listOfFiles[3].getName());
                            item4.addSelectionListener(new SelectionAdapter() {
                                public void widgetSelected(SelectionEvent e) {
                                    ProjectName.setText(listOfFiles[3].getName());
                                }});
                             break;
                      case 4:   MenuItem item5 = new MenuItem (menu, SWT.PUSH);
                            item5.setText(listOfFiles[4].getName());
                            item5.addSelectionListener(new SelectionAdapter() {
                                public void widgetSelected(SelectionEvent e) {
                                    ProjectName.setText(listOfFiles[4].getName());
                                }});
                            break;
                     }



                }

                Point pt = Save_Dialog.getLocation();
                menu.setLocation(pt.x + 280, pt.y + 100);
                menu.setVisible(true);
            }});

The code is functioning as it should, but the number of MenuItems created is dependent upon the number of cases in the switch statement. If there are more files in the location then cases, the drop-down menu will not contain the names of all the files in the location.

So here is my question. How can I create a MenuItem for every file in the location without it being dependent on the number of cases in the switch statement or any other hard-coded value?


Solution

  • Not quite sure why you need a switch here, because you can just create the MenuItems dynamically:

    public static void main(String[] args)
    {
        final Display display = Display.getDefault();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
    
        Menu bar = new Menu(shell, SWT.BAR);
        shell.setMenuBar(bar);
    
        MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
        fileItem.setText("&File");
        Menu submenu = new Menu(shell, SWT.DROP_DOWN);
        fileItem.setMenu(submenu);
    
        File dir = new File("C:\\");
    
        for (File file : dir.listFiles())
        {
            if (file.isFile())
            {
                MenuItem item = new MenuItem(submenu, SWT.NONE);
                item.setText(file.getName());
            }
        }
    
        shell.pack();
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
    }
    

    Looks like this in my case:

    enter image description here