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


Groups > comp.lang.java.gui > #3156 > unrolled thread

Singletons and Swing

Started by"Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this>
First post2011-04-27 15:43 +0000
Last post2011-04-27 15:43 +0000
Articles 7 — 4 participants

Back to article view | Back to comp.lang.java.gui


Contents

  Singletons and Swing "Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
    Re: Singletons and Swing "Peter Duniho" <peter.duniho@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 "Peter Duniho" <peter.duniho@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 "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

#3156 — Singletons and Swing

From"Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
SubjectSingletons and Swing
Message-ID<0a12ac8a-5aff-411b-8934-f855022e5466@e10g2000prf.googlegroups.com>
  To: comp.lang.java.programmer
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.

/**
 * This method initializes
 *
 */
private InsertMenu() {
 super("Insert New...");
 initialize();
}

/**
 * Provides access to the InsertMenu singleton.
 */
private static class InsertMenuHolder {
 private static InsertMenu menu = new InsertMenu();
}

/**
 * Provides access to the InsertMenu singleton from outside the
InsertMenu.
 *
 * @return the insert menu
 */
public static InsertMenu getInstance() {
	return InsertMenuHolder.menu;
}

---
 * 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

[toc] | [next] | [standalone]


#3157

From"Peter Duniho" <peter.duniho@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
Message-ID<op.t6i95v1c8jd0ej@petes-computer.local>
In reply to#3156
  To: comp.lang.java.programmer
On Thu, 14 Feb 2008 13:20:53 -0800, Jason Cavett <jason.cavett@gmail.com>  
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?

I doubt it.  Don't take my word for it, as I'm inexperienced with Java.   
But given that a Java menu is a component and has a parent, and given that  
as far as I know in Java a component can have only one parent at a time  
(that's a common enough restriction in a wide variety of parent-child code  
designs and while I haven't seen an explicit statement to that effect in  
the Java docs, it may exist and/or simply be implied), your menu can only  
be a child of any one menu at a time.

You could probably change your singleton so that it's not actually a  
menu.  Instead, it would be a sort of menu factory that keeps track of its  
actual menu instances.  It's not really clear why you want this menu to be  
a singleton anyway, but one possibility is that the menu is changeable and  
you want to be able to have just a single instance that changes and have  
those changes reflected anywhere the menu is used.  If so, you can't do it  
directly, but making your singleton a factory that tracks the created  
menus would allow the factory to also update all of the created menus  
appropriately as needed (assuming those changes always go through the  
factory, of course).

Not relevant to anything, but I'm also a little puzzled as to the reason  
for having the "InsertMenuHolder" class.  Why not just have a private  
static InsertMenu member in the singleton class itself?

Pete

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3158

From"Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
Message-ID<74523a1e-4294-4626-a555-7b1fe5a1c4b3@s19g2000prg.googlegroups.com>
In reply to#3157
  To: comp.lang.java.programmer
On Feb 14, 4:33 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Thu, 14 Feb 2008 13:20:53 -0800, Jason Cavett <jason.cav...@gmail.com>
> 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?
>
> I doubt it.  Don't take my word for it, as I'm inexperienced with Java.
> But given that a Java menu is a component and has a parent, and given that
> as far as I know in Java a component can have only one parent at a time
> (that's a common enough restriction in a wide variety of parent-child code
> designs and while I haven't seen an explicit statement to that effect in
> the Java docs, it may exist and/or simply be implied), your menu can only
> be a child of any one menu at a time.
>
> You could probably change your singleton so that it's not actually a
> menu.  Instead, it would be a sort of menu factory that keeps track of its
> actual menu instances.  It's not really clear why you want this menu to be
> a singleton anyway, but one possibility is that the menu is changeable and
> you want to be able to have just a single instance that changes and have
> those changes reflected anywhere the menu is used.  If so, you can't do it
> directly, but making your singleton a factory that tracks the created
> menus would allow the factory to also update all of the created menus
> appropriately as needed (assuming those changes always go through the
> factory, of course).
>
> Not relevant to anything, but I'm also a little puzzled as to the reason
> for having the "InsertMenuHolder" class.  Why not just have a private
> static InsertMenu member in the singleton class itself?
>
> Pete

Ahhh...good point.  I forgot about the parent thing.

The reason I want InsertMenu to be static is because I want it to
update everywhere.  Using the Observable pattern didn't really fit
well in here either.

Your suggestions seems to be a good one, except one thing.  If the
user closes a project, how can I make sure that the InsertMenu
associated with that project is destroyed?

In reference to your last question - I am lazily instantiating the
menu per: http://en.wikipedia.org/wiki/Singleton_pattern#The_solution_of_Bill_Pugh

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3159

From"Peter Duniho" <peter.duniho@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
Message-ID<op.t6jc4ohv8jd0ej@petes-computer.local>
In reply to#3158
  To: comp.lang.java.programmer
On Thu, 14 Feb 2008 14:20:03 -0800, Jason Cavett <jason.cavett@gmail.com>  
wrote:

> [...]
> Your suggestions seems to be a good one, except one thing.  If the
> user closes a project, how can I make sure that the InsertMenu
> associated with that project is destroyed?

Define "project".  But more generally, I'd say that'd be a function of the  
rest of your code.  I presume that normally, you'd just rely on the  
destruction of the frame and contained objects associated with the  
"project" to release the menu.

In this case, you'd also want to remove your menu instance from the  
collection the factory is managing, right?

Depending on how you're dealing with the destruction of the project, I  
suppose there are a variety of ways you could react to this event.  But  
one obvious answer seems to be that you could add a WindowListener to the  
frame so that you are called when it's closing or closed.  At that point,  
you'd destroy whatever resources are associated with the project,  
including the factory's reference to the menu (the menu(s)  
itself(themselves) used by the frame would be destroyed with the  
destruction of the frame, presumably...and I use the term "destroy"  
loosely since I'm not really sure at what point things get disposed versus  
just not being referenced any longer...I assume that disposing the frame  
disposes all of the contents, but like I said, I'm new to Java :) ).

I assume that in this case by "InsertMenu" you mean the actual Java menu,  
and not the factory itself (and of course that assumes we're talking about  
going with a factory implementation rather than a singleton, as a  
work-around to the "no multiple parents" issue).

> In reference to your last question - I am lazily instantiating the
> menu per:  
> http://en.wikipedia.org/wiki/Singleton_pattern#The_solution_of_Bill_Pugh

I see.  Again, I doubt it matters but...do you really need this singleton  
to be thread-safe?  I like lazy instantiation, but I think it's a bit  
clearer to write the code explicitly rather than relying on the specific  
behavior of the language.  If you don't need to synchronize access to the  
singleton, the simple "if null then instantiate" approach won't clutter up  
the code very much at all.  Maybe that's just me though.  :)

Pete

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3160

From"tar" <tar@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
Message-ID<ymi63wryxie.fsf@blackcat.isi.edu>
In reply to#3158
  To: comp.lang.java.gui
Jason Cavett <jason.cavett@gmail.com> writes:


[Snip discussion about MenuItems only being in one menu at a time]

> The reason I want InsertMenu to be static is because I want it to
> update everywhere.  Using the Observable pattern didn't really fit
> well in here either.

Well, the standard Swing approach is to use some type of data model,
which is shared among different realizations.  You then build a factory
that keeps a single data model and arranges for all of the components
that can change it to each use the same one.

Now, for menu items this is a bit trickier, since they don't normally
have an explicit model associated with them.  What is it that needs to
be updated everywhere?  What does InsertMenu do anyway?

Perhaps the answer is to build your own subclass of MenuItem that does
have a model which can be shared among instances, and that will take
care of doing the appropriate updating.

> Your suggestions seems to be a good one, except one thing.  If the
> user closes a project, how can I make sure that the InsertMenu
> associated with that project is destroyed?

Well, normally you don't need to worry about such destruction.  I
suppose if you go the factory route, you don't want to keep old items
around that are no longer needed (since that would inhibit garbage
collection).

If you went to a shared model architecture, that should largely take
care of itself, unless you had a reverse link between the model and its
realizations.

I suppose in either case, you could try making use of one of the weak
reference methods to keep one direction of reference, so that the links
wouldn't inhibit garbage collection.

On the other hand, unless there are really going to be a really big
number of these items, it might not be worth the trouble to worry
about. 


-- 
Thomas A. Russ,  USC/Information Sciences Institute

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3161

From"Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
Message-ID<47b50418$0$13844$7836cce5@newsrazor.net>
In reply to#3156
  To: comp.lang.java.programmer
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/>

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3163

From"Jason Cavett" <jason.cavett@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
Message-ID<fcd27067-7eed-4bc4-bddb-1b284b558455@d4g2000prg.googlegroups.com>
In reply to#3161
  To: comp.lang.java.programmer
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.

---
 * 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web