Invoking an action from a MouseListener on a JTable

B

Brian J. Sayatovic

I've got a JTable that I'd like to have invoke an Action when a user
double-clicks on a row. I found code on Google to get me started on
adding a MouseListener to the table, checking the click count and
calculating the clicked row. What I'm stuck on is how to invoke the
action.

My code is in such a way that I do have a reference to the Action
instance readily available. But, to invoke it, I need to call
Action.actionPerformed(ActionEvent). I'm uncertain of how to construct
the ActionEvent.

I've used the MouseEvent's source and id to construct the ActionEvent.
I've used a hard-coded action name for now ("EDIT"). But what about
the modifiers? At this time, I'm not concerned about a keymask, btu I
may be in the future.

The MouseEvent's constants for the key masks from from
java.awt.event.InputEvent constants. But the ActionEvent class defines
its own constants for the key masks. Are these two compatible?

Is there a better way to achieve my end goal of invoking the edit
Action when a row is double-clicked?

Regards,
Brian.
 
K

Kleopatra

Brian J. Sayatovic said:
I've got a JTable that I'd like to have invoke an Action when a user
double-clicks on a row. I found code on Google to get me started on
adding a MouseListener to the table, checking the click count and
calculating the clicked row. What I'm stuck on is how to invoke the
action.

My code is in such a way that I do have a reference to the Action
instance readily available. But, to invoke it, I need to call
Action.actionPerformed(ActionEvent). I'm uncertain of how to construct
the ActionEvent.

Most of the time it does not really matter - even passing a null is okay
if you are sure that the action can cope with a null (just try and see
what happens). Though if you are invoking an action from the table's
(or any other component's) actionMap then you'll need to construct a
minimal action that has the sender. Reason is that those actions often
are static and shared and therefore need the sender in the parameter to
do something meaningful. If you are still unsure have a look at doc and
source of SwingUtilities.notifyAction (there it's done for keyStrokes
but can do something close analogous for mouseEvents)


Greetings
Jeanette
 

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
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top