Paul Morrison
Paul Morrison

Reputation: 1724

Stepping through JList with up and down keys

I am displaying a JList that contains file names, in a JScrollPane. I want to select files either by clicking on a line, or by using the up and down keys.

Using the mouse works fine, but if I select one line, then click "down" or "up", it skips one entry, and selects the next line but one. I thought maybe the key was bouncing, but there is only one key event per key stroke.

I keep adding traces to the code, but I just can't see my problem!

Do I need to do something with the ListModel? Help is much appreciated!

Upvotes: 0

Views: 1265

Answers (2)

Paul Morrison
Paul Morrison

Reputation: 1724

I just wanted to make sure this question is officially answered... Kleopatra pointed out that up and down keys are handled automatically, which explains why I was skipping entries. In addition, I added a ListSelectionListener (suggested by trashgod) to add some application-specific logic to the key handling. Thanks to both of you!

Upvotes: 1

trashgod
trashgod

Reputation: 205785

You might compare your approach to this working example that uses a ListSelectionListener. In particular check your use of getValueIsAdjusting(). Also verify that you do not interfere with the existing key bindings, and do not add an unecessary KeyListener. This variation is pictured below.

image

Upvotes: 3

Related Questions