I, personally, would use a MouseMotionListener
over overriding the processMouseMotionEvent
, but that's just me.
You need some way to tell the renderer which rows are "highlighted", the two immediate ways I can think of achieving this is to ...
- Create a custom
JList
which has methods to set/get the highlighted row. You would then need to cast to this implementation and interrogate the appropriate method, taking action as required. - Provide a method within the list data that mark the row as highlighted or not. This would allow you to interrogate the data directly.
The advantage of the first approach is that it isolates the responsibility to the view, where it really belongs. It does have the disadvantage of meaning you need to create a custom JList
. It might be easier to use the getClientProperty
and putClientProperty
methods instead, this would mean you wouldn't need a custom implementation nor cast the list in the renderer, but is has the disadvantage of not being obvious to other developers.
The second approach mixes display and data information together, not something I would encourage as you really want to keep this kind of stuff separated ;)