Don't use a KeyListener
; use Key Bindings. JTable
has the following default bindings in the WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
input map:
VK_UP
has the key"Table.selectPreviousRow"
VK_DOWN
has the key"Table.selectNextRow"
.
You can replace the bindings with Action
instances that update your TableModel
as desired. This example may guide you. A complete listing can be obtained using the key binding utility cited here. Implementations may be found in the table's UI delegate, BasicTableUI
, et al. The EditorKit
actions, examined here, are also worth a look. Alternatively, consider a custom Comparator
in your RowSorter
, as shown here.