Results 1 to 4 of 4
  1. #1
    nfh
    nfh is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default Using a JMenu as a real option

    Hi,

    I'm setting up a menu where I have an "exit" menu on the right of the JMenuBar.
    I already tried to use a JMenuItem instead of a JMenu to do the job, but the L&F of the menu is different from a JMenu (the vertical gradient isn't draw) and I can't control its width.

    If I use a JMenu without any JMenuItems associated, the menu is selected when I click it and doesn't work properly.

    The most aproximate result that I was able to come up was using a JMenu for the purpose and on the selected event I change the "selected" property of the menu (with the setSelected method) to false. Unfortunately, if the user cancels the Exit option (on a confirmation dialog that pops up) I can see the "remains" of the JPopupMenu (which is empty) and if I click again on the menu, aside from the fact that the "selected" event isn't fired, the menu is selected.

    So, how would you solve this issue? How can I make an EXIT menu which allows the user to terminate the application by clicking an option on the first level of a JMenuBar?

    Best regards

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    I'd do it the way all other applications do it. That is make "Exit" a menu item of the first menu and then add an accelerator. That way you don't have to code around problems you find when you use components in a non standard way.

    But if you really want to travel the road you are on then you can try wrapping your "exit code" in a SwingUtilities.invokeLater(). This will hopefully allow the menu to be repainted in its normal state before the confirmation dialog appears.

    Or maybe you use a JButton, without Borders.

  3. #3
    nfh
    nfh is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    This is more of a theory exercise than anything else. The whole idea is to implement a menu that responds imediately instead of showing a list of menu items.

    As for the suggestion you made, I'm not experiencing any drawing issues. The "remains" of the JPopupMenu that I mentioned is normal, although I'm trying to prevent it from being shown. If you create a JMenu on a JMenuBar without any JMenuItem or other JMenu associated, you'll see a box with about 2x2 pixels where the menu options usually show.

  4. #4
    nfh
    nfh is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    I think your suggestion about using a JButton is the best option. Nonetheless, I didn't find a way to disguise the component so that it seamlessly integrates the MenuBar.

    The thing I'm missing is the visual feedback when the user clicks on the JButton. I already removed the border and set the contentAreaFilled property to false in order to prevent the button's background from being drawn. Because this property is set to false, the background of the button doesn't change when the user presses the button.

    Is there an easy way to hide everything about a button except for its behavior when pressed?

Similar Threads

  1. link between two JFrame using JMenu
    By JavaEbony in forum NetBeans
    Replies: 8
    Last Post: 03-09-2011, 11:19 AM
  2. JMenu with Tabs using swing
    By phil128 in forum AWT / Swing
    Replies: 1
    Last Post: 03-03-2009, 03:29 PM
  3. JMenu to JPopupMenu
    By carderne in forum New To Java
    Replies: 0
    Last Post: 02-28-2009, 06:07 PM
  4. JMenu calling another Forms/Panels
    By plodos in forum New To Java
    Replies: 2
    Last Post: 12-10-2007, 08:02 AM
  5. JMenu and JRadioButtonMenuItem
    By doron70 in forum AWT / Swing
    Replies: 3
    Last Post: 07-18-2007, 06:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •