Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JTable and Enter key

560 views
Skip to first unread message

Mike314

unread,
Jun 23, 2009, 3:54:37 AM6/23/09
to
Hi,

I have a standard table with all elements enabled for editing. When
I finish editing and press "enter" key the focus moves down.
How can I change this behavior to move right instead?

Thanks.

Michael Rauscher

unread,
Jun 23, 2009, 4:37:05 AM6/23/09
to

Can't see this behaviour:

import java.awt.event.*;
import javax.swing.*;

public class Test {
private void createAndShowGUI() {
JTable table = new JTable(new String[][]{
{"1,1", "1,2"},
{"2,1", "2,2"}}, new String[]{"A", "B"});
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
}

public static final void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test().createAndShowGUI();
}
});
}
}

Bye
Michael

Mike314

unread,
Jun 23, 2009, 4:57:51 AM6/23/09
to

1. Run
2. Choose first cell (1,1)
3. press some keys (like 123) to change the cell's value. (or even do
nothing - same result)
4. press enter
5. The focus changes to lower cell (2,1).

I need it to change to (1,2) instead.

Albert

unread,
Jun 23, 2009, 5:02:34 AM6/23/09
to
>
> I need it to change to (1,2) instead.

You could dig in ActionMap of the table. The action associated whith the
enter key must be in one of the 3 maps (depending of the focus).

Michael Rauscher

unread,
Jun 23, 2009, 5:55:05 AM6/23/09
to
Mike314 wrote:
> On Jun 23, 6:37 pm, Michael Rauscher <michlm...@gmx.de> wrote:
>> Mike314 wrote:
>>> Hi,
>>> I have a standard table with all elements enabled for editing. When
>>> I finish editing and press "enter" key the focus moves down.
>>> How can I change this behavior to move right instead?
>> Can't see this behaviour:
>>
> 1. Run
> 2. Choose first cell (1,1)
> 3. press some keys (like 123) to change the cell's value. (or even do
> nothing - same result)
> 4. press enter
> 5. The focus changes to lower cell (2,1).

OK, happens if one doesn't double-click or press F2 to start editing.

>
> I need it to change to (1,2) instead.

table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
"selectNextColumnCell");

Bye
Michael

John B. Matthews

unread,
Jun 23, 2009, 6:17:35 AM6/23/09
to
In article
<b5dfa6e0-26a7-40a4...@12g2000pri.googlegroups.com>,
Mike314 <mich...@gmail.com> wrote:

Use a key binding, for example:

JTable table = new JTable(...);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
InputMap map = table.getInputMap(JTable.WHEN_FOCUSED);
map.put(enter, "selectNextColumnCell");

The condition, WHEN_FOCUSED, is one of several defined in JComponent,
and the actionMapKey, "selectNextColumnCell", is one known to
BasicTableUI:

<http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Mike314

unread,
Jun 24, 2009, 3:08:49 AM6/24/09
to

Thank you very much. It is what I was looking for.

0 new messages