I have to shift the cells up/down depending upon the up/down arrow key pressed from the keyboard. I am adding a KeyListener
(in fact KeyAdapter
) on the JTable
to achieve it using the keyPressed()
method. Now what's happening is that when I am pressing the alphanumeric keys, then I am able to get the selected row using table.getSelectedRow()
, but when I am pressing the arrow keys its giving me "-1" always. That means no row is shown selected. I also tried to set the focus on table but it did not work. The code I am usin is as follows:
private void settingKeys() {
controller.getStandardActionDetailsTableEquates().addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("key pressed!!");
controller.getStandardActionDetailsTableEquates().setRowSelectionAllowed(true);
int selectedRow = controller.getStandardActionDetailsTableEquates().getSelectedRow();
System.out.println("selectedRow : " + selectedRow);
controller.getStandardActionDetailsTableEquates().requestFocusInWindow();
if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("up arrow key pressed");
Component editor = controller.getStandardActionDetailsTableEquates().getEditorComponent();
editor.requestFocusInWindow();
System.out.println("cursor : " + controller.getStandardActionDetailsTableEquates().getCursor());
System.out.println("value : " + controller.getStandardActionDetailsTableEquates().getSelectedRow());
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("down arrow key pressed");
}
}
});
}
I even tried to get the cursor position, but could not get it. Also, the editor found did not worked (shown in code). The "selected row" and "value" values are pining as "-1".
Please provide solution to this.