Swing: default action executed even with Ctrl+Enter

H

Hendrik Maryns

-----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-----
 
D

Daniele Futtorovic

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.
 
H

Hendrik Maryns

-----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-----
 
D

Daniele Futtorovic

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)
 
H

Hendrik Maryns

-----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-----
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top