1

My Code:

JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Edit circle");
JMenuItem help = new JMenuItem("Help");
JMenuItem exit = new JMenuItem("Exit");

bar.add(menu);
bar.add(help);
bar.add(exit);

Output of the JMenuBar:

OUTPUT

I want the output to be something like this:

EXPECTED_OUTPUT

What do I need to do in order to get the expected output?

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • If it were me - I'd put `Help` then a menu separator, then `Exit` in an `Application` (or similar) `JMenu` and add it to the left of the `Edit circle` menu.. I can't see that adding menu items directly to a `JMenuBar` will work out well, as your experiments suggest. – Andrew Thompson May 15 '15 at 09:27
  • 1
    `Help` and `Exit` are not `JMenu`s, they should be added to a `JMenu` and that should be added to the `JMenuBar` – MadProgrammer May 15 '15 at 09:31
  • @MadProgrammer , So, adding `JMenuItem` to a `JMenuBar` won't work? But I have [this problem](http://stackoverflow.com/questions/30255798/how-do-i-place-two-jmenuitems-adjacent-to-each-other#comment48612165_30256074) now. – Spikatrix May 15 '15 at 09:41
  • @AndrewThompson , Ok. Good idea. I'll go with that if I don't find any other way to solve my problem. – Spikatrix May 15 '15 at 09:42
  • @CoolGuy Oh, it works, but as you say, it doesn't work the way you want it to – MadProgrammer May 15 '15 at 09:42
  • again JToolBar and now JMenuBar has BoxLayout – mKorbel May 15 '15 at 11:30
  • @mKorbel , I didn't know that a layout can be applied to `JMenuBar`. Thanks for letting me know :) – Spikatrix May 15 '15 at 11:44
  • @Cool Guy you didn't read, to try code linked my comment sticked to question posted by different OP – mKorbel May 15 '15 at 14:01
  • @mKorbel . Umm. Which? [This one](http://stackoverflow.com/questions/30237571/how-to-move-a-rectangle-with-arrow-keys#comment48577445_30237837)? – Spikatrix May 15 '15 at 14:09
  • @Cool Guy [good one - sorry:-)](http://stackoverflow.com/a/8127463/714968) aaaah [my mistake](http://stackoverflow.com/questions/30196854/jseperator-in-jtoolbar-moves-the-components-to-right-end#comment48500252_30196854) – mKorbel May 15 '15 at 14:14

1 Answers1

0

you cannot add JMenuItem in JMenuBar. so try this.. it'll work..

    JMenuBar bar = new JMenuBar();
    JMenu menu1 = new JMenu("Edit circle");
    JMenu help = new JMenu("Help");
    JMenu exit = new JMenu("Exit");
    bar.add(menu1);
    bar.add(help);
    bar.add(exit);
    exit.addMenuListener(new MenuListener() {

        @Override
        public void menuSelected(MenuEvent e) {
            System.out.println("Exiting");
        }

        @Override
        public void menuDeselected(MenuEvent e) {
        }

        @Override
        public void menuCanceled(MenuEvent e) {
        }
    });

you cannot add ActionListener to JMenu. use MenuListener..

reference from this...

Community
  • 1
  • 1
ELITE
  • 5,815
  • 3
  • 19
  • 29
  • This produced the expected output, but now, the pressing `help` and `exit` doesn't do anything. I've added `ActionListener`s to both of them. :( – Spikatrix May 15 '15 at 09:38
  • `ActinoListener` will not work on `JMenu`.. for that you have to use `JMenuItem` or you can add `MenuListener` to `exit` menu...check the code... – ELITE May 15 '15 at 09:56
  • I ended up using `mouseListener`. Thanks for the help anyway. – Spikatrix May 15 '15 at 11:36