Anyone knows of a way to make a Jlist default movement cell focus behaviour change?
My jlist is one where the cells are square a开发者_Go百科nd with fixed sizes, and the view port fills up with them, so the number of columns (and row i guess) can actually change.
My code to do this is fairly complex:
http://code.google.com/p/bookjar-utils/source/browse/BookJar-utils/src/util/swing/components/ImageList.java
What i want to do is allow for different keyboard movement with the arrow keys - Instead of only using ↑ and ↓ to move up and down rows also allow ← and → if the selected cell mets the west or east edges of the viewport.
Should i just calculate how many cells are fit in a "row" and add a keylistener for the arrows that takes over when at the edges when the default behavior appear to do nothing?
For that matter where is the original behavior defined? the ← and → keys DO allow movement in a row if they are not on the edges of the row.
In Swing, every widget has its own UI-class that controls its behavior. The LookAndFeel manages these links between widget and UI (architecture overview).
So IMHO the right place to do what you ask is to extend the BasicTableUI and overwrite the method that defines the widgets ActionMap for keyboard actions, replacing some of the actions with your own implementation:
class MyTableUI extends BasicTableUI {
...
@Override
protected void installKeyboardActions() {
super.installKeyboardActions();
final ActionMap actionMap = getTable().getActionMap();
actionMap.put( NEXT_ROW,
new MyNewRowSelectionAction( NEXT_ROW, actionMap.get( NEXT_ROW ) ) );
...
Note that the keys for the ActionMap are private in BasicTableUI.Actions, so they need to be copied.
精彩评论