Swing: default action executed even with Ctrl+Enter

Discussion in 'Java' started by Hendrik Maryns, Jun 10, 2008.

  1. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi all,

    In my Swing app, I define a default button like so:

    // make submit the default action
    SwingUtilities.getRootPane(submitButton).setDefaultButton(submitButton);

    The submit button submits some query. This seems to work fine: if I hit
    enter the selected query in the UI is submitted.

    However, I also want another action, editAction to be associated with
    Ctrl+Enter, to let the user edit the query. I do the following:

    private Action createEditAction() {
    ~ final Action editAction = new AbstractAction() {
    ~ public void actionPerformed(final ActionEvent evt) {
    ~ // let the user edit the currently selected query
    ~ }

    ~ };
    ~ editAction.putValue(Action.NAME, "Edit formula…");
    ~ editAction.putValue(Action.ACCELERATOR_KEY,
    KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK));
    ~ editAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_E);
    ~ return editAction;
    }

    and later, I associate it with a menu item and ad that to a menu:

    final JMenuItem editItem = new JMenuItem(editAction);
    editMenu.add(editItem);

    For other actions I have defined like this (like Open file, Save etc.),
    this works fine, with shortcuts like Ctrl+O, Ctrl+S. But Ctrl+Enter
    executes the action associated to the default button, instead of the
    edit action. What am I doing wrong here?

    Cheers, H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.4-svn0 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iD4DBQFIToRre+7xMGD3itQRAh0SAJdX+Rv3EYCCym7Zv5qXENEN8Of4AJ9+BqJB
    WJpKtAE2NpYVOEaNpwyAjQ==
    =RROz
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Jun 10, 2008
    #1
    1. Advertising

  2. On 2008-06-10 15:40 +0100, Hendrik Maryns allegedly wrote:
    > Hi all,
    >
    > In my Swing app, I define a default button like so:
    >
    > // make submit the default action
    > SwingUtilities.getRootPane(submitButton).setDefaultButton(submitButton);
    >
    >
    > The submit button submits some query. This seems to work fine: if I
    > hit enter the selected query in the UI is submitted.
    >
    > However, I also want another action, editAction to be associated with
    > Ctrl+Enter, to let the user edit the query. I do the following:
    >
    > <snip code>
    >
    > For other actions I have defined like this (like Open file, Save
    > etc.), this works fine, with shortcuts like Ctrl+O, Ctrl+S. But
    > Ctrl+Enter executes the action associated to the default button,
    > instead of the edit action. What am I doing wrong here?


    I don't know whether this is the only way, but you should take a look at
    the UI property "RootPane.defaultButtonWindowKeyBindings".

    On my machine, UIManager.get("RootPane.defaultButtonWindowKeyBindings")
    yields a String[] containing the following values
    (index:\tgetClass().getName()\ttoString()):
    0: java.lang.String ENTER
    1: java.lang.String press
    2: java.lang.String released ENTER
    3: java.lang.String release
    4: java.lang.String ctrl ENTER
    5: java.lang.String press
    6: java.lang.String ctrl released ENTER
    7: java.lang.String release

    You might want to tinker with that Object/property.

    PS: c.l.j.gui would have seemed a more suitable place to ask this question.

    --
    DF.
    to reply privately, change the top-level domain
    in the FROM address from "invalid" to "net"
    Daniele Futtorovic, Jun 10, 2008
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Daniele Futtorovic schreef:
    | On 2008-06-10 15:40 +0100, Hendrik Maryns allegedly wrote:
    |> Hi all,
    |>
    |> In my Swing app, I define a default button like so:
    |>
    |> // make submit the default action
    |> SwingUtilities.getRootPane(submitButton).setDefaultButton(submitButton);
    |>
    |>
    |> The submit button submits some query. This seems to work fine: if I
    |> hit enter the selected query in the UI is submitted.
    |>
    |> However, I also want another action, editAction to be associated with
    |> Ctrl+Enter, to let the user edit the query. I do the following:
    |>
    |> <snip code>
    |>
    |> For other actions I have defined like this (like Open file, Save
    |> etc.), this works fine, with shortcuts like Ctrl+O, Ctrl+S. But
    |> Ctrl+Enter executes the action associated to the default button,
    |> instead of the edit action. What am I doing wrong here?
    |
    | I don't know whether this is the only way, but you should take a look at
    | the UI property "RootPane.defaultButtonWindowKeyBindings".
    |
    | On my machine, UIManager.get("RootPane.defaultButtonWindowKeyBindings")
    | yields a String[] containing the following values
    | (index:\tgetClass().getName()\ttoString()):
    | 0: java.lang.String ENTER
    | 1: java.lang.String press
    | 2: java.lang.String released ENTER
    | 3: java.lang.String release
    | 4: java.lang.String ctrl ENTER
    | 5: java.lang.String press
    | 6: java.lang.String ctrl released ENTER
    | 7: java.lang.String release
    |
    | You might want to tinker with that Object/property.

    Thanks.

    I solved the issue now by not using
    SwingUtilities.getRootPane(submitButton).setDefaultButton(submitButton);
    ~ at all and instead associating the Enter key with the submit action the
    same way I did it with the other action. That works fine now.

    | PS: c.l.j.gui would have seemed a more suitable place to ask this
    question.

    Indeed, inscribing there now.

    H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.4-svn0 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iD8DBQFITppue+7xMGD3itQRAvLfAJ4ofa1EGG8whxnnG5csJiSd02WvdACfVVTo
    r6iQSoYNyLzZFw4W2Bfh06U=
    =Uo+Y
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Jun 10, 2008
    #3
  4. On 2008-06-10 17:12 +0100, Daniele Futtorovic allegedly wrote:
    > On 2008-06-10 15:40 +0100, Hendrik Maryns allegedly wrote:
    >> Hi all,
    >>
    >> In my Swing app, I define a default button like so:
    >>
    >> // make submit the default action
    >> SwingUtilities.getRootPane(submitButton).setDefaultButton(submitButton);
    >>
    >>
    >>
    >> The submit button submits some query. This seems to work fine: if
    >> I hit enter the selected query in the UI is submitted.
    >>
    >> However, I also want another action, editAction to be associated
    >> with Ctrl+Enter, to let the user edit the query. I do the
    >> following:
    >>
    >> <snip code>
    >>
    >> For other actions I have defined like this (like Open file, Save
    >> etc.), this works fine, with shortcuts like Ctrl+O, Ctrl+S. But
    >> Ctrl+Enter executes the action associated to the default button,
    >> instead of the edit action. What am I doing wrong here?

    >
    > I don't know whether this is the only way, but you should take a look
    > at the UI property "RootPane.defaultButtonWindowKeyBindings".


    For more information as to how the value of this property is
    interpreted, see the source code for:
    javax.swing.LookAndFeel.loadKeyBindings(InputMap, Object[])
    and the javadoc for:
    java.awt.AWTKeyStroke.getAWTKeyStroke(String)


    --
    DF.
    to reply privately, change the top-level domain
    in the FROM address from "invalid" to "net"
    Daniele Futtorovic, Jun 10, 2008
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hendrik Maryns schreef:
    | Daniele Futtorovic schreef:
    | | On 2008-06-10 15:40 +0100, Hendrik Maryns allegedly wrote:
    | |> Hi all,
    | |>
    | |> In my Swing app, I define a default button like so:
    | |>
    | |> // make submit the default action
    | |>
    SwingUtilities.getRootPane(submitButton).setDefaultButton(submitButton);
    | |>
    | |>
    | |> The submit button submits some query. This seems to work fine: if I
    | |> hit enter the selected query in the UI is submitted.
    | |>
    | |> However, I also want another action, editAction to be associated with
    | |> Ctrl+Enter, to let the user edit the query. I do the following:
    | |>
    | |> <snip code>
    | |>
    | |> For other actions I have defined like this (like Open file, Save
    | |> etc.), this works fine, with shortcuts like Ctrl+O, Ctrl+S. But
    | |> Ctrl+Enter executes the action associated to the default button,
    | |> instead of the edit action. What am I doing wrong here?
    | |
    | | I don't know whether this is the only way, but you should take a look at
    | | the UI property "RootPane.defaultButtonWindowKeyBindings".
    | |
    | | On my machine, UIManager.get("RootPane.defaultButtonWindowKeyBindings")
    | | yields a String[] containing the following values
    | | (index:\tgetClass().getName()\ttoString()):
    | | 0: java.lang.String ENTER
    | | 1: java.lang.String press
    | | 2: java.lang.String released ENTER
    | | 3: java.lang.String release
    | | 4: java.lang.String ctrl ENTER
    | | 5: java.lang.String press
    | | 6: java.lang.String ctrl released ENTER
    | | 7: java.lang.String release
    | |
    | | You might want to tinker with that Object/property.
    |
    | Thanks.
    |
    | I solved the issue now by not using
    | SwingUtilities.getRootPane(submitButton).setDefaultButton(submitButton);
    | ~ at all and instead associating the Enter key with the submit action the
    | same way I did it with the other action. That works fine now.
    |
    | | PS: c.l.j.gui would have seemed a more suitable place to ask this
    | question.
    |
    | Indeed, inscribing there now.

    And I should have put a follow-up there.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.4-svn0 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iD8DBQFITqMIe+7xMGD3itQRAiDIAJoCxRy2BLdA15192NuNs3BYMOyBNwCfej/V
    XpRA977v2j07W6dbUZTRdGE=
    =0O1g
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Jun 10, 2008
    #5
    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. Danny

    Implement Ctrl-C, Ctrl-V

    Danny, Aug 14, 2003, in forum: C++
    Replies:
    5
    Views:
    638
    CBFalconer
    Aug 15, 2003
  2. Danny

    Implement Ctrl-C, Ctrl-V

    Danny, Aug 14, 2003, in forum: C Programming
    Replies:
    5
    Views:
    650
    CBFalconer
    Aug 15, 2003
  3. Angus
    Replies:
    5
    Views:
    2,088
    Michael Rauscher
    Nov 18, 2006
  4. Replies:
    2
    Views:
    1,396
    Munna
    Jul 10, 2008
  5. HopfZ
    Replies:
    0
    Views:
    541
    HopfZ
    Aug 28, 2006
Loading...

Share This Page