how to get component associated with an Action

R

robert walker

Hi all, I do not see from the Action object
how to retrieve the component that was associated
with a given action

for instance

myMenuItem = new JCheckBoxMenuItem(myAction);

at some other place in my code
I have myAction in scope and would like to ask
for a reference to myMenuItem

how can i get it?

thought there might be a
myAction.getValue(Action.COMPONENT);
type KEY



thanks all
 
A

Andrew Thompson

Hi all, I do not see from the Action object
how to retrieve the component that was associated
with a given action

Multiple components in a single UI
can have the same action (e.g. both
a JButton and a JMenuItem). So it is
not logical to think of 'the' component
associated with an Action.

[ F'Ups set to c.l.j.gui ]

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
B

Babu Kalakrishnan

[follow-ups set to comp.lang.java.gui]

robert said:
Hi all, I do not see from the Action object
how to retrieve the component that was associated
with a given action

for instance

myMenuItem = new JCheckBoxMenuItem(myAction);

at some other place in my code
I have myAction in scope and would like to ask
for a reference to myMenuItem

how can i get it?

You cannot.

If you need to refer to the component, you need to store a reference to it
elsewhere.
thought there might be a
myAction.getValue(Action.COMPONENT);
type KEY

Remember that Actions can be associated with several components. Suppose the
same action is associated with a toolbar button, a main menu item and a popup
menu item, which component would you want returned by this call ?

However, if you want to see the source component from within the actionPerformed
method of the Action, the ActionEvent argument passed to that method would
probably contain the identity of the component which activated the event.
(through the getSource() method call)

BK
 
T

Tor Iver Wilhelmsen

Hi all, I do not see from the Action object
how to retrieve the component that was associated
with a given action

Others have pointed out that an Action can be associated with many
components: Menyitems and buttons in particular.
I have myAction in scope and would like to ask
for a reference to myMenuItem

how can i get it?

In the event handler (actionPerformed()), try looking at what
getSource() returns.
 
R

robert walker

thanks all, i realize that many components can be linked to one action
obejct, but only one will trigger actionPerformed at a time, and it be
nice to
have a method like so
JComponent[] getRegisterComponentsOfAction()

I need a reference to componet(s) but I am not in the
actionperformed so the obvious getSource(0 will not do the trick
I did what one person said and just sotred it in the action object for
now

thanks all
 

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top