Changing JTable Navigational Key
I have programmed a JTable application. I want that if I press 'Enter' key in the JTable cell, the cell at right side may be selected. Similarly, when I press 'Enter' in the right most cell, the first cell of the next row may be selected.
In other words, I like 'Enter' key to behave as forward navigational key in JTable cells like 'Tab' key.
The following is the piece of code which is not working for me. Though, it changes selection border to the next cell on pressing enter, but the focus is not shifted and editing remains in the current cell.
//voucherTable is a JTable object.
Action moveForward = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
int r=voucherTable.getSelectedRow();
int c=voucherTable.getSelectedColumn();
if(c==2){
c=-1;
r+=1;
}
voucherTable.changeSelection(r,c+1,false,false);
}
};
voucherTable.getInputMap().put(KeyStroke.getKeyStr oke
(KeyEvent.VK_ENTER,0),"moveForward");
voucherTable.getActionMap().put("moveForward",
moveForward);
Kindly advise me to solve the problem.
Thanks.
Raaheel
|