Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2005

Tip: Looking for answers? Try searching our database.

How does one implement a context sensitive JMenu, depending on currently selected tab in JTabbedPane?

Thread view: 
Oliver Wong - 21 Sep 2005 15:49 GMT
   I'm writing a Swing application in which immediatly under the JFrame, I
have a JTabbedPane. Each tab of the JTabbedPane contains a JPanel with very
different sets of widgets.

   I'd like for the JMenu at the top of the JFrame to change, depending on
which tab is currently selected in the JTabbedPane. That is to say, I only
want menu entries which are relevant for the currently viewable set of
widgets to be enabled/visible/whatever.

   Is there a standard design pattern or idiom for accomplishing this
cleanly?

   - Oliver
Roedy Green - 21 Sep 2005 17:20 GMT
>    I'd like for the JMenu at the top of the JFrame to change, depending on
>which tab is currently selected in the JTabbedPane. That is to say, I only
>want menu entries which are relevant for the currently viewable set of
>widgets to be enabled/visible/whatever.

You do an addChangeListener to a hunk of code which does a
getSelectedIndex() which then sets up the corresponding menu.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Vova Reznik - 21 Sep 2005 17:49 GMT
>>   I'd like for the JMenu at the top of the JFrame to change, depending on
>>which tab is currently selected in the JTabbedPane. That is to say, I only
[quoted text clipped - 3 lines]
> You do an addChangeListener to a hunk of code which does a
> getSelectedIndex() which then sets up the corresponding menu.

Question was:

>Is there a standard design pattern or idiom for accomplishing this
>cleanly?
Roedy Green - 22 Sep 2005 00:26 GMT
> >Is there a standard design pattern or idiom for accomplishing this
> >cleanly?

so you want to now the fancy name for using a Listener?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 22 Sep 2005 01:03 GMT
>> >Is there a standard design pattern or idiom for accomplishing this
>> >cleanly?
>
>so you want to now the fancy name for using a Listener?

I think Observer covers it.

see http://en.wikipedia.org/wiki/Observer_design_pattern
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Oliver Wong - 22 Sep 2005 16:01 GMT
>>> >Is there a standard design pattern or idiom for accomplishing this
>>> >cleanly?
[quoted text clipped - 4 lines]
>
> see http://en.wikipedia.org/wiki/Observer_design_pattern

   This gave me the missing piece of the design puzzle. I was thinking with
N panels and N menu items, you'd have N^2 connections between the objects,
which could get very messy.

   What I'll probably end up doing is creating an intermediary object (a
"Sentry" object?) to which all the menu items subscribe to, and to which all
the panels ping whenever they change. That way you have N+N=2N connections,
which is much more manageable.

   Thanks.

   - Oliver
Roedy Green - 22 Sep 2005 20:31 GMT
> I think Observer covers it.
>>
>> see http://en.wikipedia.org/wiki/Observer_design_pattern
>
>    This gave me the missing piece of the design puzzle.

this is a great example of the power of design patterns.  They are way
of encapsulating and propagating "obvious" general knowledge
efficiently.

It is a nice shorthand. You can ask someone, "are you familiar with
the X design pattern?".  If not, you can point them to a canned
explanation.  If so you may not have to say anything else to help
solve the problem.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Oliver Wong - 22 Sep 2005 22:46 GMT
>> I think Observer covers it.
>>>
[quoted text clipped - 10 lines]
> explanation.  If so you may not have to say anything else to help
> solve the problem.

   This is very true, particularly when your company hires someone new and
you need to get them up to speed real quick.

Newbie: How does this work?
Oldie: You know the visitor pattern?
Newbie: Yeah.
Oldie: It's like that, except we have multiple types of trees, so we have an
abstract "super visitor", and we use Factories with generics to get the
correct visitors for each tree.
Newbie: Okay, I think I got it.
[If the reader doesn't get it, it's probably because (s)he didn't read the
javadoc package overview for the source code like the newbie did.]

   However, in the specific case of my problem being solved, I think what
psychologically cause a "click" in my mind was your use of "Observer" in the
singular, making me realize I wouldn't have to make the menu items subscribe
to each panel individually (i.e. "Observers" plural)

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.