bioMind
bioMind

Reputation: 171

JTree disable/overwrite TransferHandler keyboard actions

I need to get rid of standard keyboard action handler of TransferHandler class I use for my JTree. The JTree implements access-level mechanism that controls if node may or may not be deleted. This behavior is neglected by using keyboard combinations like shift-delete allowing each user to deletre any node from the JTree.

Basically I need the TransferHandler to provide a convenient way of moving and sorting nodes (DnD). That's it.

Thanks in advance.

Upvotes: 1

Views: 1077

Answers (2)

aterai
aterai

Reputation: 9833

Edit: I'm late, lbalazscs already suggested above.

You can try something like this:

JTree tree = new JTree();
tree.setDragEnabled(true);
tree.setDropMode(DropMode.ON_OR_INSERT);
tree.setTransferHandler(new YourTreeTransferHandler());
//......
Object key = TransferHandler.getCutAction().getValue(Action.NAME);
System.out.println(key);
tree.getActionMap().put(key, new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    System.out.println("dummy");
  }
});

Upvotes: 3

lbalazscs
lbalazscs

Reputation: 17794

You can remove key default bindings by removing them form the ActionMap

Something like this:

ActionMap actionMap = tree.getActionMap();
actionMap.remove("cut");
actionMap.getParent().remove("cut");
actionMap.remove("copy");
actionMap.getParent().remove("copy");
actionMap.remove("paste");
actionMap.getParent().remove("paste");

Upvotes: 5

Related Questions