0

I have recently start learning Java as my next language and I am getting stuck on the menu structure. In Python the order the menu items are defined is the order the menu items appear however in Java AWT I am having problems with creating the sub-menu item as my 1st item.

Even though I .add() the sub-menu items first they still appear as the last menu item. I have been looking but I am not finding any method of telling the menu to have the sub-menu be the first listed menu item.

Is this possible within AWT?

Here is the code I have showing an example of my problem. My class is names TestClass.java.

import java.awt.*;
import java.awt.event.*;


public class TestClass extends Frame{
   /**
     * 
     */
    private static final long serialVersionUID = 1L;
public TestClass () {
      setLayout(new FlowLayout());

      MenuBar mb=new MenuBar();
      Menu menu=new Menu("Menu");
      Menu submenu=new Menu("New");
      MenuItem i1=new MenuItem("Sub menu 1");
      MenuItem i2=new MenuItem("Sub menu 2");
      MenuItem i3=new MenuItem("Exit");

      submenu.add(i1);
      submenu.add(i2);
      menu.add(i3);

      menu.add(submenu);
      mb.add(menu);

      setMenuBar(mb);
      setSize(100,100);
      setLayout(null);
      setVisible(true);

      addWindowListener(new WindowAdapter() {
          @Override
          public void windowClosing(WindowEvent we) {
              System.exit(0);
           }
       }
  );

}
   public static void main(String[] args) {
       new TestClass();
   }
}
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • *"I have recently start learning Java.."* Then do yourself a favor and skip learning AWT components. Swing replaced it over a decade ago. See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. – Andrew Thompson Mar 24 '18 at 00:57
  • 2
    @AndrewThompson Yes I have already come to that conclusions. Thanks for the advice. – Mike - SMT Mar 24 '18 at 01:46

1 Answers1

2

The adding of the items to the containing menu is the important part:

  menu.add(i3);
  menu.add(submenu);

is the wrong way around. It needs to be

  menu.add(submenu);
  menu.add(i3);
daniu
  • 14,137
  • 4
  • 32
  • 53
  • Oh. Well how about that. The one variation I didn't try. Thanks for the reply. That did the trick. Got to love it when the simple things eluded you. – Mike - SMT Mar 22 '18 at 15:03
  • So why is this order important. I am not sure why adding the actual MenuItems after the actual submenu and menu make a difference. It seams like only the order of when submenu and menu have been added should matter. – Mike - SMT Mar 22 '18 at 15:06
  • Unrelated, you register a `WindowListener` to shutdown when the frame is closed. You can do the same by just `setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)`. – daniu Mar 22 '18 at 15:11
  • 1
    Oh wait. I see it now. I am still getting use to Java and everything but I just realized I was reading things wrong. The `mb.add(menu)` is not part of my issue here. Its only the `menu.add()` that is important for the order of the menu items. Its clear to me now. Thanks again daniu and thanks for the info for closing the program. I am using the Listener as it as the first thing I found when looking up how to close the program when I press the X button. In python this is built into the GUI library and can be redirected with an event binding (I believe the same concept as a Listener). – Mike - SMT Mar 22 '18 at 15:13
  • About why the order is important, the object to think about is what gets added to `menu`. It doesn't matter when the `menu` is added to the `menubar`, and it doesn't matter when the `menuitem`s are added to the `submenu`. EDIT: your comment ninja'd me. – daniu Mar 22 '18 at 15:15
  • 1
    After doing some digging I have found that `setDefaultCloseOperation()` cannot be used here as I am using AWT and not Swing. `setDefaultCloseOperation()` is Swing specific method of closing the window. – Mike - SMT Mar 22 '18 at 15:23