Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3172

Re: Singletons and Swing

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.alt.net!news-in-01.newsfeed.easynews.com!easynews.com!easynews!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail
From "Martin Gregorie" <martin.gregorie@THRWHITE.remove-dii-this>
Subject Re: Singletons and Swing
Message-ID <sfak85-qe7.ln1@zoogz.gregorie.org> (permalink)
X-Comment-To comp.lang.java.programmer
Newsgroups comp.lang.java.gui
In-Reply-To <af46378e-5377-4793-99ad-7ce9104d722a@e60g2000hsh.googlegroups.com>
References <af46378e-5377-4793-99ad-7ce9104d722a@e60g2000hsh.googlegroups.com>
Content-Type text/plain; charset=IBM437
Content-Transfer-Encoding 8bit
X-Gateway time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92]
Lines 96
Date Wed, 27 Apr 2011 15:43:19 GMT
NNTP-Posting-Host 96.60.20.240
X-Complaints-To news@tds.net
X-Trace newsreading01.news.tds.net 1303918999 96.60.20.240 (Wed, 27 Apr 2011 10:43:19 CDT)
NNTP-Posting-Date Wed, 27 Apr 2011 10:43:19 CDT
Organization TDS.net
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.gui:3172

Show key headers only | View raw


  To: comp.lang.java.programmer
Jason Cavett wrote:
> On Feb 15, 3:15 pm, Daniel Pitts
> <newsgroup.spamfil...@virtualinfinity.net> wrote:
>> Jason Cavett wrote:
>>> 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.  :-)
>> Just a hint, it is often (not always) better to show that the item is
>> still there, just not available. Hiding (rearranging) menus in any way
>> often leads to user confusion.
>>
>> --
>> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
> 
> That's a good point - not something I thought of.
> 
> As an argument, though (and, of course, there's no way you could have
> known this about my application), hiding the menu items that aren't
> available keeps the menu list from becoming exceptionally long which
> requires the user to search through a list of items.
> 
> Either way, I will make sure I do some usability testing before I make
> the context sensitive menus live.
>
IMO there are two sides to this question: you need to consider both the 
access rights of the signed-on user and the immediate context:

Access rights: never show a user any menu item he doesn't have the 
rights to use.

Context: always show the user all the menu items he;s entitled to use, 
but grey out the ones that are not valid in the current context.

Applying these two rules keeps the menu sizes under control and, equally 
important, a given user always sees the same items in every menu, but 
can't use those that are illogical and/or inappropriate in the immediate 
context.

The same rules should also apply to buttons.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Singletons and Swing "Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
  Re: Singletons and Swing "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
    Re: Singletons and Swing "Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
      Re: Singletons and Swing "Martin Gregorie" <martin.gregorie@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
        Re: Singletons and Swing "Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
          Re: Singletons and Swing "tar" <tar@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
            Re: Singletons and Swing "Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000

csiph-web