0

I'm trying to create a complete menubar using multidimensional ararys.

So far I have this code:

private JMenuBar menuBar = new JMenuBar();
private JMenuItem[][] menuItem = new JMenuItem[5][5];
private String[] menuBarItemNames = {"File", "Edit", "Database", "View", "Help"};
private String[] menuBarFileItemNames = {"Save", "Refresh", "Next", "Previous","Exit"};

view() {

    setJMenuBar(menuBar);
    for(int u = 0; u < menuItem.length; u++){

        menuItem[u][0] = new JMenu(menuBarItemNames[u]);
            for(int t = 0; t < menuBarFileItemNames.length; t++){
//Code to add 'File' child items to the 'File' MenuBarItem

            }

        menuBar.add(menuItem[u][0]);

    }

I'm struggling to figure out how to add the menuBarFileItems to the File menu.

I have this code to add to the second for loop:

menuItem[0][t] = new JMenuItem(menuBarFileItemNames[t]);

but it just causes the first item on the menuBar to be replaced by 'Save'.

Any ideas?

Also, is it not possible to have private JMenuItem[][] menuItem = new JMenuItem[5][]; so that I don't have to set the size of each menuBar item, e.g. File = 5 items, Edit = 5 items etc

Thanks

obious
  • 609
  • 8
  • 21
  • 33

2 Answers2

0

Try it:

...
  menuItem[u][0] = new JMenu(menuBarItemNames[u]);
            for(int t = 0; t < menuBarFileItemNames.length; t++){
                    menuItem[0][t].add(new JMenuItem(menuBarFileItemNames[t]));
            }
...

Att: This is only for "FILE" menu item (on zero position).

Cold
  • 787
  • 8
  • 23
0

I think what you want is this:

  private void view()
  {

    setJMenuBar(menuBar);
    for (int u = 0; u < menuItem.length; u++)
    {

      menuItem[u][0] = new JMenu(menuBarItemNames[u]);
      if (u == 0)
      {
        for (int t = 1; t <= menuBarFileItemNames.length; t++)
        {
          // Code to add 'File' child items to the 'File' MenuBarItem
          menuItem[0][t] = new JMenuItem(menuBarFileItemNames[t-1]);
          menuItem[0][0].add(menuItem[0][t]);
        }
      }

      menuBar.add(menuItem[u][0]);

    }
  }

If you're replacing the first item on the menu bar, then you're adding to the menu bar, not the menu item.

And no, Java needs to know the sizes of both dimensions of the array you're declaring, you can't leave one of them open.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Thanks, but would you mind explaining menuItem[0][0].add(menuItem[0][t])? – obious Oct 06 '13 at 13:39
  • I'm assuming that you want the 0th element of each sub-array to hold the menu, and the remaining elements to hold submenus. You add a submenu to its menu, so menuItem[0][0] represents the menu, and the submenus are in 1 through t. I wouldn't do it this way, myself -- I would have a class that held a menu and all its submenus, and have an array of those, but I was trying to fit this into the data structures you were already using. – arcy Oct 06 '13 at 13:42
  • ok, but then how would I add another set of menuItems do the 'Edit' menuBar item? menuItem[1][0].add(menuItem[0][t])? – obious Oct 06 '13 at 15:02
  • all depends on how you want to set things up; you could have a multidimensional array of strings to match the one for menus. Clearly the loop through menus has to get different values of menu items for each menu. Again, I'd've created a class to hold these, assuming you need some kind of structure for the strings, with a constructor like MenuStrings(String ... names), and create one of those per menu I wanted. I'm sorry, but without knowing how you want to structure the names you are putting on the other menu items, I'm not sure how to answer your question. – arcy Oct 06 '13 at 16:05
  • Also: this doesn't seem like enough information per menu item for a real app. When a menu item is selected, you need an indicator of what to do next (and I do NOT recommend that you test the actual string value displayed for the user (if menuitem.getText().equals("Refresh")). You're going to need a listener or a constant or something associated with the menu item itself, besides its string, to tell the program what to do on selection. Therefore the arrays of strings just seem like "demo-level" code to me, since they don't have this necessary information. – arcy Oct 06 '13 at 16:12