Accessing Sibling Objects

E

E Tepp

Hi Everyone,

Within a frame I have two objects, one is a drawing panel, and the
other is the menu bar. Once the user starts to draws I would like to
disable an item in the menu bar using the
setEnabled command. However I do not how to access the menu object
from the drawing area object. Is there a way I can do this without
using global vars.

Thanks,
Elliott
 
T

Thomas Weidenfeller

Within a frame I have two objects, one is a drawing panel, and the
other is the menu bar. Once the user starts to draws I would like to
disable an item in the menu bar using the
setEnabled command. However I do not how to access the menu object
from the drawing area object. Is there a way I can do this without
using global vars.

Yes, but it is maybe not worth the trouble.

You can try one or a combination of the following things:

- Ask GUI questions in comp.lang.java.gui :)

- Disguise the global variable using the Singleton design pattern.
The result smells more object-oriented, but it is still effectively a
global variable.

- Implement some kind of an "Application GUI" object which you can ask
for the menu item or menu bar. Of course you either have to pass the
reference to this one around, or provide it as a global variable /
singleton.

- Search the component hierarchy until you find a component that looks
like a menu bar. You will first have to navigate upwards to find the
root of your component hierarchy (most likely a frame or dialog), and
then search down from the root to find the menu bar. See SwingUtilities
for some utility methods to hely you doing this.

- You use actions for all your menu items. This alone will not change
anything. But if you list all the existing actions in one class,
you can implement a factory method with that class, designed to
give you a reference to a particular action. Then you just switch the
state of that action. The action in turn will switch the state of all
menu items, buttons, etc. where it is used.

You don't even have to provide a factory method. The most simple
implementation of such an action container class is a class with
public static members only (again, we are back at your global
variable problem, but we have now placed them all in one class):

public class Commands {
public static final Action APPL_EXIT = new ExitAction();
public static final Action EDIT_PASTE = new PasteAction();
public static final Action EDIT_CUT = new CutAction();
...
}

// Initialize a menu item using an action
JMenuItem pasteItem = new JMenuItem(Commands.EDIT_PASTE);

// Place the same command on the toolbar, too (note, Sun recommends another
// method, but this will do here)
toolbar.add(Commands.EDIT_PASTE);

// Now disable both the menu item and the toolbar item
// with one command
Commands.EDIT_PASTE.setEnabled(false);

/Thomas
 
J

John C. Bollinger

E said:
Within a frame I have two objects, one is a drawing panel, and the
other is the menu bar. Once the user starts to draws I would like to
disable an item in the menu bar using the
setEnabled command. However I do not how to access the menu object
from the drawing area object. Is there a way I can do this without
using global vars.

In addition to Thomas Weidenfeller's suggestions, I'll point out that a
standard way of doing this is to set up a Listener object (of some
appropriate sort) on the panel that holds a reference to the MenuBar /
Menu and enables/disables it in response to appropriate events.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top