On Feb 15, 12:16 am, Jason Cavett <jason.cav...@gmail.com> wrote:
On Feb 14, 10:16 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
Jason Cavett wrote:
I am attempting to design a menu system for an application I am
writing. In it, I want an InsertMenu that exists within multiple
different menus. Currently, I am attempting to do this by making the
InsertMenu a singleton. This is causing a weird issue.
I currently have two menus that hold the InsertMenu - a MainMenu and a
TreePopupMenu. The InsertMenu should be contained within both of
those. However, it seems as though it can only be in one menu at a
time. For example, if the TreePopupMenu has been created (which
happens after I've opened up a new project), the InsertMenu completely
disappears (with no errors or warnings) from the MainMenu.
Is it possible to accomplish what I'm trying to do?
Here is how I am creating my InsertMenu singleton. Could this be the
problem? Thanks.
[snip...]
In stead of sharing a menu-item instance, its common to share an Action
instance. Often the best way to do that is to extend AbstractAction.
The problem that you're seeing is that most swing components (including
JMenus, JMenuItems, etc...) know about their parent. If they are added
to a different container, they remove themselves from there other parent.
The other approach could be to have a simple method that constructs this
menu in a certain other menu (think createInsertMenu(mainMenuBar);).
it is still desirable to share Action instances (they share "disabled"
flags and icons and such).
Anyway, hope this helps,
Daniel.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Hmmm...that is a good suggestion.
The reason I didn't originally do it is because I wanted to create the
menus on the fly. But after looking at the action classes, I realized
that you can "control" the menus via the actions (L&F, icons, etc).
I'm going to have to look more closely at this.
Thanks.
Alright. My coworker and I came up with a good solution that solves
the issue and gives us context sensitive menus.
Basically, we made InsertMenu *not* a singleton and, instead,
overwrote JMenuItem so that, when it is enabled or disabled, it is
also set visible/invisible, respectively (overwrote "setEnabled" and
"setVisible" so they stay consistent). That way, when the Action (we
have an ActionFactory) is enabled or disable, it will carry over to
our JMenuItem.
It works beautifully and is much better than having a Singleton of the
InsertMenu.
Thanks for the help. :-)
still there, just not available. Hiding (rearranging) menus in any way
often leads to user confusion.