Invoking an action from a MouseListener on a JTable

Discussion in 'Java' started by Brian J. Sayatovic, Jul 31, 2003.

  1. 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.
    Brian J. Sayatovic, Jul 31, 2003
    #1
    1. Advertising

  2. Brian J. Sayatovic

    Kleopatra Guest

    "Brian J. Sayatovic" wrote:
    >
    > 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
    Kleopatra, Jul 31, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter the Swede

    MouseListener and hiding components.

    Peter the Swede, Nov 19, 2003, in forum: Java
    Replies:
    0
    Views:
    345
    Peter the Swede
    Nov 19, 2003
  2. Minti
    Replies:
    2
    Views:
    458
    Ian Shef
    Feb 19, 2004
  3. Cammy

    Newbie MouseListener Problem

    Cammy, Apr 9, 2004, in forum: Java
    Replies:
    2
    Views:
    509
    Knute Johnson
    Apr 10, 2004
  4. Jenny

    Interface MouseListener

    Jenny, Aug 19, 2004, in forum: Java
    Replies:
    1
    Views:
    341
    Lee Weiner
    Aug 20, 2004
  5. Jenny

    Interface MouseListener

    Jenny, Aug 19, 2004, in forum: Java
    Replies:
    3
    Views:
    449
    Andrew Thompson
    Aug 20, 2004
Loading...

Share This Page