Question

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.

Was it helpful?

Solution 2

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");
  }
});

OTHER TIPS

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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top