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.
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
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.
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).
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
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>
Thank you very much. It is what I was looking for.