I have a JTable with actually 4 columns and many rows. Now i want to select one or more rows by pressig a key on the keyboard. But i have no idea how do that.
This is the example of what i want:
- if i press "F" on keyboard my application should select or hightlight the first row in my table which has an entry beginning with the char "F" (or the other i pressed
- if i press the same key again (here "F"), then is should select or highlight te next row which has an entry beginning with the char "F"
- if the last row is selected and i press the same key again it should select or highlight the first row again (looping)
- when i press another key, for example "U", it should do the same with the rows beginning with "U" like above.
This sounded like an interesting problem. So I hacked up something which can be the basis of your solution. Note: You can improve this further with proper encapsulation and generics. Warning : use this only if your table data model is small. If it is big you should consider some better approach for search as well as concurrency/UI feedback to avoid freezing of your UI. Anyway, the solution will be in similar lines as below. The key is to add a key listener to the table, and then checking key press character against row/col values. HTH