facebook

Understanding jMenuItem

  1. MyEclipse Archived
  2.  > 
  3. Matisse/Swing UI Development
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #285630 Reply

    Douglas M Hurst
    Participant

    I added a jMenu(jMenu1) to my form.
    I then added a jMenuItem (jMenuItem1) to jMenu1. The text for this jMenuItem is Exit.

    I added

    private void jMenuItem1MouseClicked(java.awt.event.MouseEvent evt) {
    System.exit(0);
    }

    … which DOES NOT execute when the jMenuItem (jMenuItem1 (text: Exit)) is clicked.

    I then added

    private void jMenuItem1MousePressed(java.awt.event.MouseEvent evt) {
    System.exit(0);
    }

    … which DOES when the jMenuItem (jMenuItem1 (text: Exit)) is clicked and causes the program to exit gracefully.

    I also, in designer mode, double-clicked on the accelerator key for jMenuItem1 and set it to Alt-X. When I run the program, Alt-X has no effect.

    Unfortunately, the Matisse4MyEclipse (M4M) Tutorial doesn’t touch on jMenu, which would seem to be a pretty basic addition to most any Matisse/Swing main jFrame.

    Is there another tutorial that you know of that would touch on these topics?

    #285634 Reply

    Riyad Kalla
    Member

    Doug,
    These are actually Swing issues, not M4M-specific issues.

    The first problem is that you are support to set the actionPerformed event on MenuItems, not handle the mouse events directly because different LNFs and different components may intercept or handle the mouse events differently. The “actionPerformed” event is always fired when a item is clicked though.

    Also the actionPerformed is what is executed when the keystroke is matched, which is why setting the mouse event is skpping that and making it seem like a no-op.

    I created an example project for you that shows you what ou were trying to do. You can import it, run it and see what i mean. You might need to adjust your JRE lib in the build path to match whichever one is correct in your setup, then just run MainFrame and you can hit Alt-X to exit.

    Attachments:
    You must be logged in to view attached files.
    #285653 Reply

    Douglas M Hurst
    Participant

    Got it thanks!

    So many years using Borland’s C++ Builder.

    #285662 Reply

    Riyad Kalla
    Member

    No problem, glad it helped.

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Understanding jMenuItem

This topic is marked as closed to new replies, however your posting capabilities still allow you to do so.

You must be logged in to post in the forum log in