Reputation: 5262
As developing a Swing application I use Action
rather than Listener
. I have an action which is usually triggered from AbstractButtons
but now I need to trigger it as well when selecting a JTree
node.
Since the TreeSelectionModel
does not support Actions I am wondering what the best practice would be to archive this.
Until now I just came up with the idea to implement a generalized method which is invoked from actionPerformed(ActionEvent)
and as well from addTreeSelectionListener(TreeSelectListener)
.
Anyone who has a better idea?
Upvotes: 0
Views: 383
Reputation: 205785
This seems like it might increase coupling between your TreeModel
and its view component, JTree
. As Action
instances are often used in key bindings, it may help to review the existing actions defined for common Look & Feel implementations, as shown in @camickr's article Key Bindings. These actions can be evoked from your TreeSelectionListener
, as shown in this example that leverages scroll pane actions.
Addendum: For reference, javax.swing.plaf.metal.MetalLookAndFeel
defines these JTree
actions and bindings.
Action Focused Ancestor ------ ------ ------- addToSelection SPACE cancel ESCAPE clearSelection ctrl BACK_SLASH collapse SUBTRACT copy ctrl INSERT, ctrl C, COPY cut CUT, shift DELETE, ctrl X expand ADD extendTo shift SPACE moveSelectionTo shift ctrl SPACE moveSelectionToParent paste shift INSERT, ctrl V, PASTE scrollDownChangeLead ctrl PAGE_DOWN scrollDownChangeSelection PAGE_DOWN scrollDownExtendSelection shift ctrl PAGE_DOWN, shift PAGE_DOWN scrollLeft ctrl LEFT, ctrl KP_LEFT scrollLeftChangeLead scrollLeftExtendSelection scrollRight ctrl RIGHT, ctrl KP_RIGHT scrollRightChangeLead scrollRightExtendSelection scrollUpChangeLead ctrl PAGE_UP scrollUpChangeSelection PAGE_UP scrollUpExtendSelection shift PAGE_UP, shift ctrl PAGE_UP selectAll ctrl SLASH, ctrl A selectChild RIGHT, KP_RIGHT selectChildChangeLead selectFirst HOME selectFirstChangeLead ctrl HOME selectFirstExtendSelection shift HOME, shift ctrl HOME selectLast END selectLastChangeLead ctrl END selectLastExtendSelection shift END, shift ctrl END selectNext DOWN, KP_DOWN selectNextChangeLead ctrl DOWN, ctrl KP_DOWN selectNextExtendSelection shift ctrl DOWN, shift DOWN, shift ctrl KP_DOWN, shift KP_DOWN selectParent LEFT, KP_LEFT selectParentChangeLead selectPrevious KP_UP, UP selectPreviousChangeLead ctrl UP, ctrl KP_UP selectPreviousExtendSelection shift UP, shift KP_UP, shift ctrl UP, shift ctrl KP_UP startEditing F2 toggle toggleAndAnchor ctrl SPACE
Upvotes: 2